C ++ Image Processing-PCX Format Image (bottom)

Source: Internet
Author: User

Reading Tips:

The C ++ image processing series focuses on code clarity and readability, all using C ++ code.

Delphi Image ProcessingThe series focuses on efficiency. The general code is Pascal, and the core code is BaSm.

Make sure that the two items are consistent and can be compared with each other.

 

C ++ Image Processing-PCX Format Image (I) converts a PCX format image to a GDI + bitmap. This article describes how to convert a GDI + bitmap to a PCX Format Image.

The following is the code for converting a GDI + bitmap to a PCX image:

Int packpcxline (lpbyte DEST, lpbyte source, int bytespreline, int planes) {lpbyte Pd = DEST; int Delta = planes --; lpbyte PS = source + planes; int bytes = bytespreline; while (planes> = 0) {int COUNT = 0; byte c = * pS; do {count ++; If (-- bytes = 0) {If (-- planes <0) break; bytes = bytespreline; PS = source + planes;} else PS + = delta ;} while (C = * PS & count <0x3f); If (C> = 0xc0 | count> 1) * PD ++ = cou NT | 0xc0; * PD ++ = C;} return Pd-Dest;} // revoke typedef Union {word value; struct {Byte Low; byte high ;};} testmask; int packpcx4line (lpbyte DEST, lpbyte source, int bytespreline, int width) {int bytes = bytespreline <2; lpbyte Buf = DEST + bytes; testmask mask; mask. value = 0x1001; width = (width + 1)> 1; while (mask. high) {byte C = 0; Te bit = 0x80; lpbyte Pb = Buf; For (INT I = 0; I <width; I ++) {If (source [I] & Mask. high) c | = bit; bit >>=1; If (source [I] & Mask. low) c | = bit; bit >>=1; If (bit = 0) {* pb ++ = C; C = 0; bit = 0x80 ;}} BUF + = bytespreline; mask. value <= 1;} return packpcxline (DEST, DEST + bytes, bytes, 1);} // define void argbquadtorgbtriple (prgbtriple d EST, prgbquad source, int count) {for (INT I = 0; I <count; I ++) {Dest [I]. rgbtblue = source [I]. rgbred; Dest [I]. rgbtgreen = source [I]. rgbgreen; Dest [I]. rgbtred = source [I]. rgbblue ;}}// your bool savepcximagetostream (istream * stream, bitmap * BMP) {pixelformat format = BMP-> getpixelformat (); If (format = pixelformatundefined) return Fal Se; pcxfileheader header; colorpalette * pal = NULL; byte palette [256*3 + 1]; zeromemory (& header, sizeof (pcxfileheader); header. bitsprepixel = getpixelformatsize (format); header. planes = 1; if (header. bitsprepixel> 8) {format = pixelformat24bpprgb; header. planes = 3; header. bitsprepixel = 8;} else // If (header. bitsprepixel> 1) {pal = (colorpalette *) New byte [256 * sizeof (argb) + sizeof (colorpalette)]; BMP-> Getpalette (PAL, BMP-> getpalettesize (); prgbtriple ppal = (prgbtriple) & palette [1]; // if it is a 16-color bitmap, save the palette to the file header if (format = pixelformat4bppindexed) {header. planes = header. bitsprepixel; header. bitsprepixel = 1; ppal = (prgbtriple) header. palette;} argbquadtorgbtriple (ppal, (prgbquad) Pal-> entries, pal-> count); Delete [] pal;} gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight (); header. flag = 0x0a; header. versio N = 5; header. encodeing = 1; header. xmax = R. width-1; header. ymax = R. height-1; header. hres = 96; header. vres = 96; header. palettetype = 1; header. bytespreline = (R. width * header. bitsprepixel + 7)> 3; if (header. bytespreline & 1) header. bytespreline ++; // Save the PCX File Header if (Stream-> write (& header, sizeof (pcxfileheader), null )! = S_ OK) return false; // obtain the GDI + bitmap data in place graph data structure bitmapdata data; data. stride = (R. width * getpixelformatsize (Format) + 31) &-32)> 3; int size = R. height * data. stride; // size is the number of bytes of bitmap data, header. bytespreline * header. planes * 2 is the number of bytes in the encoding buffer. scan0 = (lpvoid) New byte [size + header. bytespreline * header. planes * 2]; BMP-> lockbits (& R, imagelockmoderead | imagelockmodeuserinputbuf, format, & data); BMP-> unlockbits (& data ); // if the first item of the monochrome bitmap palette is not 0, the bitmap data is reversed if (format = pixelformat1bppindexed & (* (argb *) & palette [1] & 0 xffffff )) {int COUNT = data. height * (data. stride> 2); lpdword Pd = (lpdword) data. scan0; For (INT I = 0; I <count; Pd [I] ^ = (DWORD) (-1), I ++);} lpbyte P = (lpbyte) data. scan0; lpbyte buffer = P + size; int bytes; // encode RLE row by row and save it to the stream for (uint y = 0; y <data. height; y ++, P + = data. stride) {If (format = pixelformat4bppindexed) bytes = packpcx4line (buffer, P, header. bytespreline, Data. width); elsebytes = packpcxline (buffer, P, header. bytespreline, header. planes); stream-> write (buffer, bytes, null);} Delete [] data. scan0; // if it is a 256-color bitmap, the color palette is saved to the end of the stream if (format = pixelformat8bppindexed) {palette [0] = 0x0c; stream-> write (palette, 256*3 + 1, null);} return true;} // describool savepcximagetofile (lptstr filename, bitmap * BMP) {istream * stream = new filestream (filename, false ); stream-> addref (); bool result = savepcximagetostream (stream, BMP); stream-> release (); return result;} // response ;}//---------------------------------------------------------------------------

In the code, the savepcximagetostream function has commented on the general conversion process. This article is no longer arrogant, the savepcximagetofile function still uses the simple file stream I wrote to save the converted PCX format image to the file, filestream is used in the previous Article C ++ Image Processing-PCX Format Image (I.

The following is an example of converting a GDI + bitmap to a PCX image (bcb2010 ):

void __fastcall TForm1::Button3Click(TObject *Sender){Gdiplus::Bitmap *bmp = new Gdiplus::Bitmap(L"d:\\1-4.bmp");if (bmp->GetLastStatus() != Ok)throw new Exception("Load Image fail");Gdiplus::Graphics *g = new Gdiplus::Graphics(Canvas->Handle);g->DrawImage(bmp, 0, 0);delete g;SavePcxImageToFile("d:\\1-4.pcx", bmp);delete bmp;}

Similarly, for the PCX file format, search for related documents online.

 

Due to limited levels, errors are inevitable. Correction and guidance are welcome. Email Address:Maozefa@hotmail.com

Here, you can access "C ++ Image Processing-Article Index".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.