void PLBmpDecoder::GetImage (PLBmpBase & Bmp) { if (GetBitsPerPixel() == 8) Bmp.SetPalette (&m_Pal[0]); switch (m_pBMI->biBitCount) { case 1: decode1bpp (m_pDataSrc, &Bmp); break; case 4: if (m_pBMI->biCompression == BI_RGB) decode4bpp (m_pDataSrc, &Bmp); else decodeRLE4 (m_pDataSrc, &Bmp); break; case 8: if (m_pBMI->biCompression == BI_RGB) decode8bpp (m_pDataSrc, &Bmp); else decodeRLE8 (m_pDataSrc, &Bmp); break; case 16: decodeHiColor (m_pDataSrc, &Bmp, m_pBMI); break; case 24: case 32: decodeTrueColor (m_pDataSrc, &Bmp, m_pBMI->biBitCount); break; default: // This is not a standard bmp file. raiseError (PL_ERRFORMAT_UNKNOWN, "Decoding bmp: Illegal bpp value."); } }
void PLPSDDecoder::GetImage ( PLBmpBase& Bmp ) { // Was Open called? PLASSERT (m_pDataSrc); if (GetBitsPerPixel() == 8) Bmp.SetPalette (m_pPal); try { skipLayerData (m_pDataSrc); readImageData (m_pDataSrc, &Bmp, m_PSDHeader.Mode, m_PSDHeader.Rows, m_PSDHeader.Columns, m_PSDHeader.Channels); } catch (PLTextException) { cleanup (); Close(); throw; } cleanup (); }
void PLSGIDecoder::readUncompressed ( PLBmpBase & Bmp ) { if (GetBitsPerPixel() == 8) { PLBYTE ** ppLine = Bmp.GetLineArray(); for (int y = 0; y < GetHeight(); y++) { memcpy(ppLine[GetHeight()-y-1], m_pDataSrc->ReadNBytes(GetWidth()), GetWidth()); } } else { PLBYTE ** ppLine = Bmp.GetLineArray(); for (int z = 0; z < m_Header.ZSize; z++) { for (int y = GetHeight()-1; y >=0 ; y--) { PLBYTE * pLine = ppLine[y]; for (int x = 0; x < GetWidth(); x++) { switch (z) { case 0: pLine[x*4+PL_RGBA_RED] = ReadByte (m_pDataSrc); break; case 1: pLine[x*4+PL_RGBA_GREEN] = ReadByte (m_pDataSrc); break; case 2: pLine[x*4+PL_RGBA_BLUE] = ReadByte (m_pDataSrc); break; case 3: pLine[x*4+PL_RGBA_ALPHA] = ReadByte (m_pDataSrc); break; } } } } } }
void PLPNGDecoder::GetImage(PLBmpBase &bmp) { if(m_color_type == PNG_COLOR_TYPE_GRAY) { const int numColors = 1<<(m_bit_depth); for(int i = 0; i < numColors; i++) { const int curColor = (i*255)/(numColors-1); bmp.SetPaletteEntry(i, curColor, curColor, curColor, 0xFF); } } if(m_color_type == PNG_COLOR_TYPE_PALETTE) { png_color *ppng_color_tab = NULL; int nbColor = 0; png_get_PLTE(m_png_ptr, m_info_ptr, &ppng_color_tab, &nbColor); for(int i = 0; i < nbColor; i++, ppng_color_tab++) { bmp.SetPaletteEntry(i ,ppng_color_tab->red ,ppng_color_tab->green ,ppng_color_tab->blue ,0xFF); } } if(m_bit_depth == 16) { png_set_strip_16(m_png_ptr); } if(m_bit_depth < 8) { png_set_packing(m_png_ptr); } PLBYTE **pLineArray = bmp.GetLineArray(); png_read_image(m_png_ptr, pLineArray); /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ png_read_end(m_png_ptr, m_info_ptr); /* clean up after the read, and free any memory allocated - REQUIRED */ png_destroy_read_struct(&m_png_ptr, &m_info_ptr, (png_infopp)NULL); }
void PLSubBmp::Create( PLBmpBase & SrcBmp, const PLRect & SrcRect) { PLASSERT (!m_pLineArray); int bpp = SrcBmp.GetBitsPerPixel(); if (bpp <= 8) m_pClrTab = new PLPixel32[(int)(1 << bpp)]; else m_pClrTab = NULL; initLocals (SrcRect.Width(), SrcRect.Height(), SrcBmp.GetPixelFormat()); if (bpp <= 8) SetPalette (SrcBmp.GetPalette()); m_pLineArray = new PLBYTE * [m_Size.y]; PLBYTE** ppSrcLines = SrcBmp.GetLineArray(); int XOfs = SrcRect.tl.x*(bpp/8); for (int y=0; y<m_Size.y; y++) m_pLineArray[y] = ppSrcLines[SrcRect.tl.y+y]+XOfs; PLASSERT_VALID(this); }
void PLWEMFDecoder::GetImage (PLBmpBase & Bmp) { HPALETTE hpal = NULL; LPLOGPALETTE plogpal (0); if (GetBitsPerPixel() == 8) { plogpal = (LPLOGPALETTE)new PLBYTE[sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256)]; memset(plogpal,0x0,sizeof(LOGPALETTE) + sizeof(PALETTEENTRY)*256); plogpal->palVersion = 0x300; plogpal->palNumEntries = 256; if (plogpal == NULL) { PLASSERT(false); raiseError (PL_ERRNO_MEMORY,"Cannot allocate palette."); } UINT pe = GetEnhMetaFilePaletteEntries(m_hemf, 0, NULL); GetEnhMetaFilePaletteEntries(m_hemf, pe, plogpal->palPalEntry); } // Setup a logical palette for our memory dc and also a // paintlib compatible palette for the paintlib bitmap PLPixel32 pPal[256]; if (plogpal) { for (UINT i = 0; i < 256; i++) { pPal[i] = *(PLPixel32*)&plogpal->palPalEntry[i]; } if ((hpal = CreatePalette((LPLOGPALETTE)plogpal))) { m_holdpal = SelectPalette(m_memdc, hpal, false); RealizePalette(m_memdc); } Bmp.SetPalette(pPal); delete [] plogpal; } // Play the metafile into the device context // First, setup a bounding rectangle and fill // the memory dc with white (some metafiles only // use a black pen to draw and have no actual fill // color set, This would cause a black on black // painting which is rather useless RECT rc; rc.left = rc.top = 0; rc.bottom = GetHeight(); rc.right = GetWidth(); FillRect(m_memdc,&rc,(HBRUSH)GetStockObject(WHITE_BRUSH)); // Heeere we go.... BOOL bres = PlayEnhMetaFile(m_memdc,m_hemf,&rc); // Finally, convert the Windows bitmap into a paintlib bitmap PLWinBmp& winbmp = dynamic_cast<PLWinBmp&>(Bmp); BITMAPINFO* pBMI = (BITMAPINFO*)winbmp.GetBMI(); PLBYTE* pBits = (PLBYTE*)winbmp.GetBits(); if (GetBitsPerPixel() == 8) { GetDIBits(m_dc, m_bm, 0, GetHeight(), winbmp.GetBits(), pBMI, DIB_RGB_COLORS); } else { GetDIBits(m_dc, m_bm, 0, GetHeight(), winbmp.GetBits(), pBMI, DIB_PAL_COLORS); } }
void PLIFF85Decoder::GetImage(PLBmpBase & Bmp) { Trace(2, "Decoding IFF-85 body.\n"); if (GetBitsPerPixel() == 8) { Bmp.SetPalette(&m_pal[0]); } // Decode each row into local storage, further processing depends on // form type. const int bytes_per_row = getBytesPerRow(); std::vector<PLBYTE> buf(bytes_per_row); for (int row = 0; row < m_bitmapHeader.h; ++row) { #if DETAILED_TRACE std::ostringstream strTrace; strTrace << "#Row " << row << ".\n"; Trace(3, strTrace.str().c_str()); #endif if (m_bitmapHeader.compression == PLIFF85::cmpByteRun1) { readCompressedRow(&buf[0], m_pDataSrc, bytes_per_row); } else { PLASSERT(m_bitmapHeader.compression == PLIFF85::cmpNone); readUncompressedRow(&buf[0], m_pDataSrc, bytes_per_row); } std::vector<PLBYTE> decodedBuf(m_bitmapHeader.w * GetBitsPerPixel() / 8); if (m_formType == PLIFF85::ID_PBM) { // The number of bytes we want to output may be less than the number // we have read in, as the input must have an even number of bytes per // plane (which can produce a lot of unnecessary padding for a PBM). const int bytes_per_row = m_bitmapHeader.w * GetBitsPerPixel() / 8; decodedBuf.assign(buf.begin(), buf.begin() + bytes_per_row); } else { PLASSERT(m_formType == PLIFF85::ID_ILBM); for (int bp = 0; bp < m_bitmapHeader.nPlanes; ++bp) { const int start_index = bp * bytes_per_row / m_bitmapHeader.nPlanes; for (int i = 0; i < m_bitmapHeader.w; ++i) { // Get the byte we're interested in. PLBYTE the_byte = buf[start_index + i / 8]; // Isolate the bit we're interested in. the_byte &= static_cast<PLBYTE> (1 << (7 - (i % 8))); // Now shift the bit to the correct position for this plane. if ((7 - (i % 8)) > bp) { the_byte >>= 7 - (i % 8) - bp; } else { the_byte <<= bp - (7 - (i % 8)); } decodedBuf[i] |= the_byte; } } }
void PLPCXDecoder::GetImage (PLBmpBase & Bmp) { int i, x, y; PLBYTE ColorMap[PCX_MAXCOLORS][3]; PLBYTE * pcximage = NULL; PLBYTE * lpHead1 = NULL; PLBYTE * lpHead2 = NULL; PLBYTE * pcxplanes; PLBYTE * pcxpixels; PLBYTE c; int nbytes, count; Trace (2, "PCX getimage.\n"); try { nbytes = m_PcxHeader.BytesPerLine * m_PcxHeader.ColorPlanes * GetHeight(); lpHead1 = pcximage = (PLBYTE *)malloc(nbytes); try { while (nbytes > 0) { c = ReadByte(m_pDataSrc); if ((c & 0XC0) != 0XC0) // Repeated group { *pcximage++ = c; --nbytes; continue; } count = c & 0X3F; // extract count c = ReadByte(m_pDataSrc); if (count > nbytes) { raiseError(PL_ERRINTERNAL, "repeat count spans end of image."); } nbytes -= count; while (--count >=0) *pcximage++ = c; } } catch (PLTextException e) { // Just in case BytesPerLine is bogus. // This will fall apart for images that have a palette after the // image data, however. if (e.GetCode() != PL_ERREND_OF_FILE) throw; } pcximage = lpHead1; for (i = 0; i < 16; i++) { ColorMap[i][0] = m_PcxHeader.ColorMap[i][0]; ColorMap[i][1] = m_PcxHeader.ColorMap[i][1]; ColorMap[i][2] = m_PcxHeader.ColorMap[i][2]; } if (m_PcxHeader.BitsPerPixel == 8 && m_PcxHeader.ColorPlanes == 1) { /* It seems like valid PCXs exist with a bad color map signature... PLBYTE colsig = ReadByte(m_pDataSrc); if (colsig != PCX_256_COLORS) { raiseError(PL_ERRINTERNAL, "bad color map signature."); } */ for (i = 0; i < PCX_MAXCOLORS; i++) { ColorMap[i][0] = ReadByte(m_pDataSrc); ColorMap[i][1] = ReadByte(m_pDataSrc); ColorMap[i][2] = ReadByte(m_pDataSrc); } } if (m_PcxHeader.BitsPerPixel == 1 && m_PcxHeader.ColorPlanes == 1) { ColorMap[0][0] = ColorMap[0][1] = ColorMap[0][2] = 0; ColorMap[1][0] = ColorMap[1][1] = ColorMap[1][2] = 255; } lpHead2 = pcxpixels = (PLBYTE *)malloc(GetWidth() + m_PcxHeader.BytesPerLine * 8); // Convert the image PLPixel32 ** pLineArray = Bmp.GetLineArray32(); for (y = 0; y < GetHeight(); y++) { pcxpixels = lpHead2; pcxplanes = pcximage + (y * m_PcxHeader.BytesPerLine * m_PcxHeader.ColorPlanes); if (m_PcxHeader.ColorPlanes == 3 && m_PcxHeader.BitsPerPixel == 8) { // Deal with 24 bit color image for (x = 0; x < GetWidth(); x++) { PLPixel32 * pPixel = pLineArray[y]; pPixel[x].Set (pcxplanes[x], pcxplanes[m_PcxHeader.BytesPerLine + x], pcxplanes[2*m_PcxHeader.BytesPerLine + x], 0xFF); } continue; } else if (m_PcxHeader.ColorPlanes == 1) { PCX_UnpackPixels(pcxpixels, pcxplanes, m_PcxHeader.BytesPerLine, m_PcxHeader.ColorPlanes, m_PcxHeader.BitsPerPixel); } else { PCX_PlanesToPixels(pcxpixels, pcxplanes, m_PcxHeader.BytesPerLine, m_PcxHeader.ColorPlanes, m_PcxHeader.BitsPerPixel); } for (x = 0; x < GetWidth(); x++) { i = pcxpixels[x]; PLPixel32 * pPixel = pLineArray[y]; pPixel[x].Set (ColorMap[i][0], ColorMap[i][1], ColorMap[i][2], 0xFF); } } } catch (PLTextException) { if (lpHead1) { free(lpHead1); lpHead1 = NULL; } if (lpHead2) { free(lpHead2); lpHead2 = NULL; } throw; } if (lpHead1) { free(lpHead1); lpHead1 = NULL; } if (lpHead2) { free(lpHead2); lpHead2 = NULL; } }
void PLSGIDecoder::readRLE ( PLBmpBase & Bmp ) { //int xsize = m_Header.XSize; int ysize = m_Header.YSize; int zsize = m_Header.ZSize; /* size of rle offset and length tables */ int tablen = ysize * zsize * sizeof(long); unsigned long * RowOffsets = (unsigned long *)malloc(tablen); for (int i = 0; i<ysize*zsize; i++) RowOffsets[i] = ReadMLong(m_pDataSrc); /* read run length table */ unsigned long * RowLengths = (unsigned long *)malloc(tablen); for (int i = 0; i<ysize*zsize; i++) RowLengths[i] = ReadMLong(m_pDataSrc); for (int channel = 0; channel < zsize; channel++) { int ChannelOffset = 0; if (zsize == 1) ChannelOffset = 0; else switch(channel) { case 0: ChannelOffset = PL_RGBA_RED; break; case 1: ChannelOffset = PL_RGBA_GREEN; break; case 2: ChannelOffset = PL_RGBA_BLUE; break; case 3: ChannelOffset = PL_RGBA_ALPHA; break; } for (int y = 0; y < ysize; y++) { unsigned long SourceOffset = RowOffsets[y+channel*ysize]; PLBYTE * pDestPixels = Bmp.GetLineArray()[Bmp.GetHeight()-y-1]; m_pDataSrc->Seek(SourceOffset); int RowLen = RowLengths[y+channel*ysize]; PLBYTE * pSrcLine = m_pDataSrc->ReadNBytes(RowLen); PLBYTE * pSrcData = pSrcLine; bool bDone = false; while (!bDone && pSrcData-pSrcLine != RowLen) { PLBYTE Count = *pSrcData++; if (Count == 0) bDone = true; else { if (Count & 0x80) { Count &= 0x7F; while (Count--) { *(pDestPixels+ChannelOffset) = *pSrcData++; pDestPixels+=Bmp.GetBitsPerPixel()/8; } } else { PLBYTE Pixel = *pSrcData++; while (Count--) { *(pDestPixels+ChannelOffset) = Pixel; pDestPixels+=Bmp.GetBitsPerPixel()/8; } } } } } } delete[] RowOffsets; delete[] RowLengths; }