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
/ ************************************************** **************************** 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
============================================================================================================================================================================================================= ========= 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] --------