public byte[] convertDDM2ByteArrayforDisplay(dtm dm, GeoImageData gd) { byte[] byOut = new byte[gd.Ncols * gd.Nrows]; Int32 ind = 0; float elevdiff = dm.ElevMax - dm.ElevMin; for (int i = 0; i < gd.Nrows; i++) { for (int j = 0; j < gd.Ncols; j++) { float val = 255F * ((dm.dem[j, i] - dm.ElevMin) / elevdiff); byOut[ind] = (byte)val; ind++; } } return(byOut); }
public byte[] getOneBandBytes(int whichBand) // get the desired band content from a bil, ENVI, tif, jpg file to byte array { int numBytes = gida.Nbits / 8; byte[] byteOut; try { byteOut = new byte[gida.Ncols * gida.Nrows]; // * numBytes } catch (Exception) { System.Windows.Forms.MessageBox.Show("Memory overflow. Too big image"); return(null); } if (gida.FileType == GeoImageData.fTypes.BIL || gida.FileType == GeoImageData.fTypes.BSQ || gida.FileType == GeoImageData.fTypes.ENVI) // if data file type is BIL, BSQ or ENVI { if (gida.Interleave == "BSQ" || gida.FileType == GeoImageData.fTypes.BSQ) // if data interleave is BSQ { using (FileStream fs = new FileStream(gida.FileName, FileMode.Open, FileAccess.Read)) { BinaryReader br = new BinaryReader(fs); if ((gida.Nbits == 8)) { Int64 ind = 0; Int64 startPos = 0; startPos = (Int64)((whichBand) * gida.Ncols * gida.Nrows); fs.Position = startPos; for (int i = 0; i < gida.Nrows; i++) { for (int j = 0; j < gida.Ncols; j++) { byteOut[ind] = br.ReadByte(); ind++; } } } if ((gida.Nbits == 16)) { int offs = 0; Int64 ind = 0; Int64 startPos = offs; startPos = (Int64)((whichBand) * gida.Ncols * gida.Nrows) * numBytes; fs.Position = startPos; for (int i = 0; i < gida.Nrows; i++) { for (int j = 0; j < gida.Ncols; j++) { Int32 b = br.ReadUInt16(); byteOut[ind] = Convert.ToByte(b / 256); ind++; } } } } } else // if data interleave is BIL or ENVI { using (FileStream fs = new FileStream(gida.FileName, FileMode.Open, FileAccess.Read)) { BinaryReader br = new BinaryReader(fs); if ((gida.Nbits == 8)) { Int64 ind = 0; Int64 startPos = 0; for (int i = 0; i < gida.Nrows; i++) { startPos = (Int64)((gida.Nbands * i + whichBand) * gida.Ncols); fs.Position = startPos; for (int j = 0; j < gida.Ncols; j++) { byteOut[ind] = br.ReadByte(); ind++; } } } if ((gida.Nbits == 16)) { int offs = 0; Int64 ind = 0; Int64 startPos = offs; for (int i = 0; i < gida.Nrows; i++) { startPos = (Int64)((gida.Nbands * i + whichBand) * gida.Ncols) * numBytes; fs.Position = startPos; for (int j = 0; j < gida.Ncols; j++) { Int32 b = br.ReadUInt16(); byteOut[ind] = Convert.ToByte(b / 256); ind++; } } } } //if (gida.Nbits==12) //{ // int offs = 0; // Int64 ind = 0; // Int64 startPos = offs; // for (int i = 0; i < gida.Nrows; i++) // { // startPos = (Int64)((gida.Nbands * i + whichBand) * gida.Ncols) * numBytes; // fs.Position = startPos; // for (int j = 0; j < gida.Ncols; j++) // { // Int32 b = br.ReadByte(); // byteOut[ind] = Convert.ToByte(b / 16); // ind++; // } // } //} } } if (gida.FileType == GeoImageData.fTypes.GWH) // if data is in a GWR file { string gwrFile = Path.GetDirectoryName(gida.FileName) + "\\" + Path.GetFileNameWithoutExtension(gida.FileName) + "\\" + whichBand + ".gwr"; byteOut = File.ReadAllBytes(gwrFile); } if (gida.FileType == GeoImageData.fTypes.TIF) // if data is in a TIF file { //byteOut = getOneBandBytesFromStreem(whichBand); if (gida.Nbands == 3 && gida.Nbits == 8) // if the image has 24 bit color depth { byteOut = getOneBandtoByteArrayFromBitmap(new Bitmap(gida.FileName), whichBand); } if (gida.Nbands == 1 && gida.Nbits == 8) // if data has grayscale color (8 bits, 1 band) { byteOut = getOneBandBytesFromStream(whichBand); // if the TIF file has more than 3 bands } else { byteOut = getOneBandBytesFromStream(whichBand); // if the TIF file has more than 3 bands and Nbits is 16 } } if (gida.FileType == GeoImageData.fTypes.JPG) // if data is in a JPG file { if (gida.Nbands == 3 && gida.Nbits == 8) // if the image has 24 bit color depth { byteOut = getOneBandtoByteArrayFromBitmap(new Bitmap(gida.FileName), whichBand); } else { //byteOut = getOneBandBytesFromStreem(whichBand); // if the JPG file has more than 3 bands byteOut = getOneBandtoByteArrayFromBitmap(new Bitmap(gida.FileName), whichBand); } } if (gida.FileType == GeoImageData.fTypes.DDM) //------------------ { if (gida.Nbits == 16) { dtm ddm = new dtm(); ddm.FileName = gida.FileName; byteOut = getOneBandtoByteArrayFromBitmap(ddm.demBitmap, whichBand); } } return(byteOut); }