PCX image file format reading and writing

zhaozj2021-02-16  46

PCX image file format reading and writing

The PCX image file format first appears in Zsoft's development of PC Paintbrush plot software, because the drawing software is powerful and successfully transplanted to the Windows operating system, plus PCX is one of the earliest colored image formats, PCX is currently more popular Image format.

For developing image browsing, processing software programmers, how to read, save the PCX image format is the most basic topic, according to its own understanding of the PCX image format, I hope to be useful to the reader, Due to the constraints, the file format is not introduced here, the reader can refer to the relevant number.

The code is as follows, and the method is simply explained after the method segment, the level is limited, please include: my electronic mail box is: cadinfo@263.net, welcome to discuss.

============================================================================================================================================================================================================= ====== / *************************************************** ********************************* Function Name: loadpcxline (PPCXHead PPCXHDR, LPBYTE PPCXIMG, LPBYTE PPCXBITS) Const * * Parameters: PPCXHead PPCXHDR - Point points to PCXHEAD structure! NULL, import BitPlane, Byteperline, => clscanlinesize * lpbyte PPCXIMG - point to PCX image area pointer! NULL, RLE compression coding, location increment = REC. * Before calling the first address pointer: * lpbyte PPCXBITS - points to the pointer of the DIB data area, increment by scanning (scanline) length ** Return: UINT REC - Return to the number of bytes after each line decompression ** Description: According to PCX Decoding of RLE ************************************************************************************************************************************* ****************************************** / uint CpcxImage :: loadpcxline (PPCXHEAD PPCXHDR, LPBYTE PPCXIMG, LPBYTE PPCXBITS) Const {Assert (PPCXHDR! = NULL && PPCXIMG! = NULL && PPCXBITS! = NULL);

// Because in Bitmap Bits Order, It's Blue => Green => Red // HOWEVER PCX ​​IS Red => Green => Blue So Use Decrease Order // ---------------- ------------------------- uint lpos (0), // Record the total number of PPCXBITS ix (0), // Record each bit Flat byte number REC (0); // Read _ppcximg_ byte sequence for (int bp = ppcxhdr-> bitplane-1; bp> = 0; bp -) {// rle decoding ====== = Ix = 0; while (ix byteperline) {byte uizalue = ppcximg [REC ]; if ((uiValue & 0xc0) == 0xc0) // Determine whether the high byte sets 0xc0 {uiValue = uivalue & 0x3f; // Calculate Repeat Byte Color = PPCXIMG [REC ]; // Extract Color // Store to Memory Dib for (BRE BREPEAT = 0; Brepeat BitPlane bp] = Color; lpos ;}} else {PPCXBITS [(ix ) * PPCXHDR-> Bitplane bp] = uivalue; lpos ;}}} RETURN REC;}}}

/ ************************************************** **************************** Function Name: PackPCXLINE (PPCXHead PPCXHDR, LPBYTE PPCXIMG, LPBYTE PPCXBITS) Const ** Parameters: PPCXHead PPCXHDR - Pointing to the PCXHEAD structure! NULL, import bitplane, byteperline, => clscanlinesize * lpbyte PPCXBITS - Pointer to the DIB data area, increment * lpbyte PPCXIMG, point to the PCX image area pointer! NULL, RLE compression coding. * Before calling: lpbyte ppcximg = new byte [2 * bitplane * Byteperline] ** Return: UINT REC - Return the number of bytes after each row compression ** Description: RE COD according to DIB image data pointer (test The algorithm is very perfect, support 256 and 24bit true color) ********************************************** ********************************************** / UINT CPCXIMAGE :: PackPCXLINE (PPCXHead PPCXHDR, LPBYTE PPCXBITS, LPBYTE PPCXIMG) Const {// -------------------------------------- // RLE Compress Assert (PPCXHDR! = Null && ppcxbits! = Null && ppcximg! = Null); Byte i (1); uint LPOS (0), REC (0);

// ☆ RLE encoding, maximum repeat <= 63 ☆ for (int bp = ppcxhdr-> bitplane-1; bp> = 0; bp - {lpos = 0; // processed RGB sequence

While (LPOS BYTEPERLINE) / / is equivalent to less than image width {i = 1; // Reset step size - 1 // ---------------> The following code checks while (PPCXBITS [(i-1 lpos) * PPCXHDR-> BitPlane bp] == PPCXBITS [(i lpos) * PPCXHDR-> BitPlane bp]) && ((LPOS i) Byteperline) && (i <63)) i ; if (i> 1 && i <64) {// indicates that there is a repetitive pixel value to start now, write to PCX image data buffer // 1. Repeat PPCXIMG [REC ] = i | 0xc0; // 2. Pixel value PPCXIMG [REC ] = PPCXBITS [LPOS * PPCXHDR-> BitPlane BP]; LPOS = i; // LPOS - Record the current scanning line The number of bytes already processed // REC - records the number of bytes currently written to the PCX file} Else {// indicates that there is no repetitive pixel value // pixel value greater than 0xc0 (192), write Sign 0xC1 IF ((PPCXBITS [LPOS * PPCXHDR-> BitPlane BP] & 0xc0) == 0xc0) PPCXIMG [REC ] = 0xc1; PPCXIMG [REC ] = PPCXBITS [LPOS * PPCXHDR-> Bitplane bp]; lpos ;}} } // Write image data end RETURN Rec;}

============================================================================================================================================================================================================= ========= Call is as follows: 1.// rLE decoding -------------> Already included 8,24bit image for (int = 0; IY <= PPCXHDR) -> YMax; iY ) {ZeroMemory (ppcxBits, clScanLineSize); ppcxImg = LoadPCXLine (ppcxHdr, ppcxImg, ppcxBits); // read the scan line data ppcxBits = clScanLineSize;} ppcxHdr pointing PCXHEAD structure (128Byte) pointer, ppcxBits is To store the buffer of the decoded image data, PPCXIMG is a pointer to the image data in the PCX image file, which is incremented by the scan line. The completion function is an image data to decode image data from the PCX file to the Windows bitmap format. 2.// rLE compression -------------> It has included 2 times buffer for 8,24bit images // worst case, neighboring is not repeated, and is greater than 0xc0 lpbyte PPCXIMG = New byte [2 * pcxhdr.bitplane * pcxhdr.byteperline]; // Store temporary scan line UINT REC (0); // counter, write like PCX file bytes for (int = 0; IY <= PCXHDR) .YMax; iY ) {ZeroMemory (ppcxImg, 2 * pcxHdr.BitPlane * pcxHdr.BytePerLine); rec = PackPCXLine (& pcxHdr, ppcxBits, ppcxImg); increment ppcxBits = clScanLineSize // DIB scanning line; pFile-> Write (ppcxImg, rec );} Delete [] PPCXIMG; specific parameters are substantially the same as 1.ppcximg is a temporary RLE compressed buffer. ------ [OVER] --------

转载请注明原文地址:https://www.9cbs.com/read-27272.html

New Post(0)