| Rendering Buffer Rendering Buffer is a memory block used to store image data. This is a bridge between the memory and the monitor. We need to display the memory image to identify the memory block and display it using the system API (in fact, there is almost no need to do the conversion work, because the image storage format used by the API is compatible with Rendering Buffer in both Windows and Linux ). Header file:#include "agg_rendering_buffer.h" Type:Rendering_buffer Constructor: rendering_buffer(int8u* buf, unsigned width, unsigned height, int stride); The parameter represents the memory block pointer, width, height, and stride of each row respectively (when the stride is <0, it indicates that the stride is upside down) Member method:
| VoidAttach(Int8u * Buf, unsigned width, unsigned height, int STRIDE ); |
The parameters are the same as those of the constructor. |
| Int8u *Buf(); |
Return memory block pointer |
UnsignedWidth() Const; UnsignedHeight() Const; IntStride() Const; UnsignedStride_abs() Const; |
Returns the width, height, and stride of each row. |
| Int8u *Row_ptr(INT y) |
Returns the pointer pointing to the starting point of row y. |
| VoidClear(Int8u value) |
Fill the entire memory block with the value |
| Template <class renbuf> voidCopy_from(Const renbuf & SRC) |
Copy data from another rendering_buffer |
Lab code (based on the code here)Add the following at the end of the on_draw () method:
- Rows: int8u * P = rbuf. row_ptr (20); // get the 20th rows pointer.
- Memset (p, 0, rbuf. stride_abs (); // fill the whole row with 0
The resulting figure is: External and GDI display
The image storage method of Rendering Buffer is the same as that of Windows BMP, so it is very easy for the handler to process BMP. The following Code demonstrates how to display the handler on HDC.
- # Include <agg_rendering_buffer.h>
- # Include <agg_pixfmt_rgba.h>
- # Include <agg_renderer_base.h>
- # Include <agg_rasterizer_scanline_aa.h>
- # Include <agg_scanline_p.h>
- ...
- // First, let the system generate a 32-bit BMP Cache
- Bitmapinfo BMP _info;
- BMP _info.bmiheader.bisize = sizeof (bitmapinfoheader );
- BMP _info.bmiheader.biwidth = width;
- BMP _info.bmiheader.biheight = height;
- BMP _info.bmiheader.biplanes = 1;
- BMP _info.bmiheader.bibitcount = 32;
- BMP _info.bmiheader.bicompression = bi_rgb;
- BMP _info.bmiheader.bisizeimage = 0;
- BMP _info.bmiheader.bixpelspermeter = 0;
- BMP _info.bmiheader.biypelspermeter = 0;
- BMP _info.bmiheader.biclrused = 0;
- BMP _info.bmiheader.biclrimportant = 0;
- HDC mem_dc =: createcompatibledc (HDC );
- Void * Buf = 0;
- Hbitmap BMP =: createdibsection (
- Mem_dc,
- & BMP _info,
- Dib_rgb_colors,
- & Buf,
- 0,
- 0
- );
- // Associate BMP with mem_dc so that the worker can work with native GDI.
- Hbitmap temp = (hbitmap): SelectObject (mem_dc, BMP );
- // ================================================ ======================================
- // The following is the response code
- Expiration: rendering_buffer rbuf;
- // 32-Bit Bitmap. The number of bytes in each row is width * 4.
- // BMP is inverted. In order to be the same as that of GDI, the last parameter is a negative value.
- Rbuf. Attach (unsigned char *) BUF, width, height,-width * 4 );
- // Pixel format and renderer_base
- Authorization: pixfmt_bgra32 pixf (rbuf );
- Expiration: renderer_base <Expiration: pixfmt_bgra32> renb (pixf );
- Renb. Clear (partition: rgba8 (255,255,255,255 ));
- // Scanline Renderer
- Principal: renderer_scanline_aa_solid <principal: renderer_base <principal: pixfmt_bgra32> Ren (renb );
- // Rasterizer & scanline
- Authorization: rasterizer_scanline_aa <> RAS;
- Authorization: scanline_p8 SL;
- // Multidefinition line (triangle)
- RAS. move_to_d (20.7, 34.15 );
- RAS. line_to_d (398.23, 123.43 );
- RAS. line_to_d (165.45, 401.87 );
- // Set the color before rendering
- Ren. color (gradient: rgba8 (80, 90, 60 ));
- Expiration: render_scanlines (Ras, SL, Ren );
- // ================================================ ======================================
- // Display BMP to HDC. If the image contains an alpha channel, use alphablend instead of bitblt.
- : Bitblt (
- HDC,
- Rt. Left,
- Rt. Top,
- Width,
- Height,
- Mem_dc,
- 0,
- 0,
- Srccopy
- );
- // Release resources
- : SelectObject (mem_dc, temp );
- : Deleteobject (BMP );
- : Deleteobject (mem_dc );
The resulting figure is:Use the pixel_map class provided by workersIf you think the above method is still a little annoying (it is too troublesome to blame Ms's API), you can consider using the pixel_map class provided by ghost friendship, it is much easier to use it to operate BMP. (Add [encoding]/src/platform/Win32/agg_win32_bmp .cpp for compilation)
- # Include <agg_rendering_buffer.h>
- # Include <agg_pixfmt_rgba.h>
- # Include <agg_renderer_base.h>
- # Include <agg_rasterizer_scanline_aa.h>
- # Include <agg_scanline_p.h>
- # Include <platform/Win32/agg_win32_bmp .h>
- ...
- Crect RC;
- Getclientrect (& rc );
- Usage: pixel_map PM;
- PM. Create (RC. Right, RC. Bottom, callback: org_color32 );
- // ================================================ ======================================
- // The following is the response code
- Expiration: rendering_buffer rbuf;
- Rbuf. Attach (PM. Buf (), PM. Width (), PM. Height (),-PM. stride ());
- // Pixel format and renderer_base
- Authorization: pixfmt_bgra32 pixf (rbuf );
- Expiration: renderer_base <Expiration: pixfmt_bgra32> renb (pixf );
- Renb. Clear (partition: rgba8 (255,255,255,255 ));
- // Scanline Renderer
- Principal: renderer_scanline_aa_solid <principal: renderer_base <principal: pixfmt_bgra32> Ren (renb );
- // Rasterizer & scanline
- Authorization: rasterizer_scanline_aa <> RAS;
- Authorization: scanline_p8 SL;
- // Multidefinition line (triangle)
- RAS. move_to_d (20.7, 34.15 );
- RAS. line_to_d (398.23, 123.43 );
- RAS. line_to_d (165.45, 401.87 );
- // Set the color before rendering
- Ren. color (gradient: rgba8 (80, 90, 60 ));
- Expiration: render_scanlines (Ras, SL, Ren );
- // ================================================ ======================================
- PM. Draw (HDC );
Pending Author: Mao Source: www.cppprog.com |