The following code reads TIFF files in VC:
Char * szfilename = "K: \ map \ fujiian-Dem \ fujian1.tif"; tiff * tiff = open (szfilename, "R"); // open the TIFF file, get the pointer. All subsequent operations will be performed using the pointer int ntotalframe = tiffnumberofdirectories (TIFF); // get the total number of frames of the image // export setdirectory (TIFF, 0 ); // open the first image, that is, 0th frames. If it is 1st frames, write 1 as the second parameter, and so on. Because the basic operations on Windows images are in the BMP format, we read the frame and convert it to the BMP format. Char * dtitle; tiffgetfield (TIFF, tifftag_pagename, & dtitle); // obtain the frame name, which is stored in dtitle. Int width, height; tiffgetfield (TIFF, tifftag_imagewidth, & width); // obtain the width of tiffgetfield (TIFF, tifftag_imagelength, & height); // obtain the height float resolution = max (width, height); uint16 bitspersample = 1; uint16 samplesperpixel = 1; 1_getfield (TIFF, tifftag_samplesperpixel, & samplesperpixel); // The number of machine words in each pixel. The 24-bit samplesperpixel must be 3. Tiffgetfield (TIFF, tifftag_bitspersample, & bitspersample); // The font length of each machine, which must be 8. Uint16 bitsperpixel = bitspersample * samplesperpixel; // calculate the number of bits per pixel, 24 bitmap, value: 24 DWORD dwbyteperline = (width * bitsperpixel + 31)/32*4; // calculate the number of bytes in each line of the Image Based on the preceding parameters. Dword64 dwleng = height * dwbyteperline; // The length required to store the image data in the memory. * pdata = new byte [dwleng]; // allocate memory space for data storage uint32 * raster; uint32 * row; raster = (uint32 *) malloc (width * height * sizeof (uint32); tiffreadrgbaimage (TIFF, width, height, (uint32 *) pdata, 1); // read the data of the frame from the above rows and save it to raster. Row = & raster [0]; lpbyte bits2 = pdata; For (INT y = 0; y
The following code is opened with gdal
Char * szfilename = "K: \ map \ fujiian-Dem \ fujian1.tif"; gdaldataset * podataset; // gdalallregister (); podataset = (gdaldataset *) gdalopen (szfilename, ga_readonly); If (podataset = NULL) {afxmessagebox (_ T ("file opening failed !!! "); Return;} gdalrasterband * poband; // a remote sensing band int nbandcount = podataset-> getrastercount (); poband = podataset-> getrasterband (1 ); // It is a little different from the array subscript // get the size of the image display window getclientrect (& m_viewrect); int nimgsizex = podataset-> getrasterxsize (); int transform = podataset-> getrasterysize (); double adfgeotransform [6]; podataset-> getgeotransform (adfgeotransform); double right = adfgeotransform [0] + nimgsizex * adfgeotransform [1] double Bottom = adfgeotransform [3] + nimgsizey * adfgeotransform [5]; int encrypted, encrypted; encrypted = nimgsizex; nbuffersizey = nimgsizey; int nscrrenwidth = m_viewrect.width (); int nscrrenheight = m_viewrect.height (); byte * pafscanblock1, * templock1; pafscanblock1 = (byte *) cplmalloc (nscrrenwidth) * (nscrrenheight); templock1 = pafscanblock1; poband-> rasterio (gf_read, 0, 0, nbuffersizex, nbuffersizey, pafscanblock1, nscrrenwidth, nscrrenheight, gdt_byte, 0, 0 ); // display the image DWORD dwbytes = (nscrrenwidth * 24)/8 point by point in view; while (DWORD) dwbytes) % 4) {dwbytes ++ ;} byte * szbuffer = new byte [nscrrenheight * dwbytes]; memset (szbuffer, 0, nscrrenheight * dwbytes); byte * ptemp = szbuffer; cclientdc DC (this); int nindex = 0; for (INT I = 0; I <nscrrenheight; I ++) {for (Int J = 0; j <nscrrenwidth; j ++) {byte dn1 = * pafscanblock1; memcpy (szbuffer, (char *) (& dn1), 1); szbuffer + = 1; pafscanblock1 ++;} szbuffer = ptemp + dwbytes * I;} cplfree (templock1 ); bitmapinfoheader bmihdr; bitmapinfo MapInfo; memset (& bmihdr, 0, sizeof (bitmapinfoheader); bmihdr. bibitcount = 3*8; bmihdr. biclrimportant = 0; bmihdr. biclrused = 0; bmihdr. bicompression = bi_rgb; bmihdr. biheight =-nscrrenheight; bmihdr. biplanes = 1; bmihdr. bisize = sizeof (bitmapinfoheader); bmihdr. bisizeimage = 0; bmihdr. biwidth = nscrrenwidth; bmihdr. bixpelspermeter = 0; bmihdr. biypelspermeter = 0; MapInfo. bmiheader = bmihdr; MapInfo. bmicolors [0]. rgbblue = 0; MapInfo. bmicolors [0]. rgbgreen = 0; MapInfo. bmicolors [0]. rgbred = 0; MapInfo. bmicolors [0]. rgbreserved = 0; DC. setstretchbltmode (maxstretchbltmode);: stretchdibits (DC. getsafehdc (), 0, 0, nscrrenwidth, nscrrenheight, 0, 0, bmihdr. biwidth,-bmihdr. biheight, ptemp, (lpbitmapinfo) (& MapInfo), dib_rgb_colors, srccopy); gdalclose (podataset); Delete [] ptemp;