In order to quickly display large images, we have been learning gdal recently. gdal is indeed a powerful open-source library with core data sets and bandwidths, the following figure details the relationship between them and the details:
The rasterio function of gdal is very powerful. A short sentence can be used to display images, but it is also a simple function that makes me confused. In the course of using gdal, I am very grateful to Li Lin and Yan Jianming for their tireless answers to my questions and key points, so that I can get twice the result with half the effort in solving the problem.
Now I want to write down my experiences in the recent learning process and leave them to myself for future reference. I also hope to help new users!
Gdalallregister ();
Const Char * STR;
M_pdataset = (Gdaldataset * ) Gdalopen (STR, ga_readonly );
This part should be well understood. First, to open an image, we must register its driver, because each type of raster data has a different driver, that is, the driver. Then we can use the gdalopen () function to open a dataset. If the open format is not supported by gdal, we needNewGdaldriver () creates a new driver and sets it to enable it. If the gdalopen () function returns NULL, opening the file fails.
After opening a dataset, we can operate on the internal information of the dataset. The details of dtaset and its internal relationships are described in great detail.M_pdataset->Getgeotransform (m_adfgeotransform );
Above lineCodeM_adfgeotransform is an array containing six elements. After executing this line of code, we can obtain information about the m_adfgeotransform array. m_adfgeotransform [0], m_adfgeotransform [3] is the coordinate of the sitting angle of the entire image, m_adfgeotransform [1] is the resolution of the image width, m_adfgeotransform [5] is the resolution of the Image Height, for m_adfgeotransform [2] and m_adfgeotransform [4], if the image is Northern, the values of these two parameters are 0. With these parameters, we can use the following two lines of code to find the information in the lower left corner and upper right corner of the image:Xmin=M_adfgeotransform [0];
Ymin=M_adfgeotransform [3];
Xmax = M_adfgeotransform [ 0 ] + NX * M_adfgeotransform [ 1 ] + M_adfgeotransform [ 2 ];
Ymax = M_adfgeotransform [ 3 ] + M_adfgeotransform [ 4 ] + NY * M_adfgeotransform [ 5 ];
With the image information, what we need to do is to display the image. Here there is a resolution problem. One is the image resolution, and the other is the display resolution. The image resolution is the number of pixels per unit length, and the display resolution is the number of pixels that can be displayed on the output device (such as a display screen or printer) and the distance between the displayed pixels. These two resolutions are very useful when displaying images. Understanding the principles is indeed the cornerstone of success!
Then we get to the core of gdal: Pbuffer = (Byte * ) Cplmalloc ( Sizeof (Byte) * (Xbuf) * (Ybuf) * Nband );
M_pdataset -> Rasterio (gf_read, xsrc, ysrc, xwidth, yhight, pbuffer, xbuf, ybuf, gdt_byte, nband, null, nband, 0 , 1 );
For images with more than three bands, we can read the pbuffer memory directly using the above two lines of code. Then, based on our needs, such as OpenGL, we can use gldrawpixels () this function directly shows the image. Now the key point is the several parameters in rasterio. Compare with the official gdal function description:
Gdaldataset: rasterio (gdalrwflag erwflag,
Int Nxoff,
Int Nyoff,
Int Nxsize,
Int Nysize,
Void * Pdata,
Int Nbufxsize,
Int Nbufysize,
Gdaldatatype ebuftype,
Int Nbandcount,
Int * Panbandmap,
Int Npixelspace,
Int Nlinespace,
Int Nbandspace
)
If gf_read is used, the image content is written to the memory. If gf_write is used, the content in the memory is written to the file.
The four parameters nxoff, nyoff, nxsize, and nysize are used for images. nxoff and nyoff mean that we need to get data starting from the pixel coordinate of the image. nxsize, nysize is the width and height of the data from the image. That is to say, the range of data to be retrieved from the image is (nxoff, nyoff) to (nxoff + nxsize, nyoff + nysize ). If we want to display the obtained range, we need to write this data field to the cache pdata. nbufxsize and nbufysize are the size range of this cache area.
The rasterio function only provides these descriptions. We need to calculate it based on our actual needs during application. If there is a m image, you want to quickly display it, the setting of these six parameters is critical, but there is a precondition that the image is a pyramid image, so that you have set the buffer size, then, gdal automatically scales the data of nxsize and nysizen to the buffer of bufxsize and nbufysize Based on the implementation process of the gdal internal function ), you do not need to control which overview to obtain. At first, I did not understand the true meaning of this function, and many detours were taken on the display of large images, I decided to read the first layer of the pyramid, So that I had to crash myself. I am very grateful to Li Lin for the details, he also patiently gave me some examples of abstract things and added his own time to finally understand the principle of this function. Thank you, Brother Li.
The size range of nbufxsize and nbufysize in the cache area determines the size of the screen to display the image content you want to display, therefore, we need to calculate the buffer size based on the screen range and zoom ratio.
Npixelspace indicates the byte size of a pixel. If it is 24 bits, a pixel occupies 3 bytes, so npixelspace = 3. Nlinespace indicates the size of a row. If it is set to 0, the default value is nbufxsize * nbufysize.
Nbandspace is the size of data in a band. If it is set to 0, it is stored in the form of rrrgggbbb. If it is set to 1, it is stored in the form of rgbrgbrgb.
However, this function still needs to be tried to understand its true meaning. I hope that new users will not take a detour !! In the future, I will add my learning experiences, ^_^ fighting !!! O (partition _ partition) o...