Some tips about 2d visualization based on image.
RGBs and RsGsBs
When we look at one colored image with width*height
pixels, there are actually three layers (red, green and blue). We can get colorful results when we blend these three values properly on screen.
There are two typical ways to store each pixel value, store results separately, such as RRRR…GGGG…BBBB we use RGBs to represent it, we store each channel separately. Another way is store it by RGBs namelly RGBRGBRGB… we need to know the how the data is stored in memory before we give image to specific paiting library.
Number of channels
Typical way is RGB for three channels and RGBA for four channels. The A value represents the alpha value which shows the transparency of each pixel. Using RGBA as an example, there are usually 4*8bit=32bit=4B
for each pixle.
Using Cario as 2d painter example
Depending on little endian and small endian, the sequence of data can be different. For small endian, the first data is put from low memory address to high memory address. For big endian mode, the data is put into the high memory address firstly and then small address.
For cario RGB24, and the data is small endian, we actually put bbbbbbbbggggggggrrrrrrrr in memory (left is high memory address and right is low memory address)
TODO, add cario little small endian pattern
PPM data format
If we just want to input/output data for simple checking, we can use PPM as format, which is just naive way to write out the RGBA data format without encoding etc.
This is a simple code snippet for read the ppm data based on icet, if the original data buffer stores the rgb, we use offset=3 here, otherwise, if original data struct is rgba, we use offset=4. We can see that when writting out the ppm data, we do not include the alpha value in it (or assuming alpha is 1)
void write_ppm(const char *filename, unsigned char *image, int width, int height, int offset) |
以下是从ppm文件读取RGBA buffer的相关代码,动态决定alpha值,对于背景部分,设置alpha值为0,对于非背景部分,设置alpha值为255:
|
NC data
NC data can be viewed as a kind of rectilinear grid. NC data itsself is flexible, but in geo related simulation, each point in 2d is a combination of latitude and longitude, we can simply use the Panoply to show the results, and use image show in python script to draw the results directly.