GIFLoader (InputStream& in)
        : input (in),
          dataBlockIsZero (false), fresh (false), finished (false),
          currentBit (0), lastBit (0), lastByteIndex (0),
          codeSize (0), setCodeSize (0), maxCode (0), maxCodeSize (0),
          firstcode (0), oldcode (0), clearCode (0), endCode (0)
    {
        int imageWidth, imageHeight;
        if (! getSizeFromHeader (imageWidth, imageHeight))
            return;

        uint8 buf [16];
        if (in.read (buf, 3) != 3)
            return;

        int numColours = 2 << (buf[0] & 7);
        int transparent = -1;

        if ((buf[0] & 0x80) != 0)
            readPalette (numColours);

        for (;;)
        {
            if (input.read (buf, 1) != 1 || buf[0] == ';')
                break;

            if (buf[0] == '!')
            {
                if (readExtension (transparent))
                    continue;

                break;
            }

            if (buf[0] != ',')
                continue;

            if (input.read (buf, 9) == 9)
            {
                imageWidth  = (int) ByteOrder::littleEndianShort (buf + 4);
                imageHeight = (int) ByteOrder::littleEndianShort (buf + 6);

                numColours = 2 << (buf[8] & 7);

                if ((buf[8] & 0x80) != 0)
                    if (! readPalette (numColours))
                        break;

                image = Image (transparent >= 0 ? Image::ARGB : Image::RGB,
                               imageWidth, imageHeight, transparent >= 0);

                image.getProperties()->set ("originalImageHadAlpha", transparent >= 0);

                readImage ((buf[8] & 0x40) != 0, transparent);
            }

            break;
        }
    }
Example #2
0
int main(int argc, char* argv[])
{
	struct options opts;
	int ret;
	uqm_palette_t pal;

	parse_arguments(argc, argv, &opts);
	verbose_level = opts.verbose;

	verbose(2, "Processing '%s', index %d-%d\n", opts.infile, opts.from, opts.to);

	memset(&pal, 0, sizeof(pal));

	ret = readPalette(&pal, opts.infile);
	if (ret != 0)
		return ret;

	if (opts.luma < 0 && !opts.add && !opts.sub && !opts.blend)
		setColor(&pal, opts.from, opts.to, &opts.color);

	if (opts.luma >= 0)
	{
		if (opts.color_set && !opts.add && !opts.sub && !opts.blend)
			setColorLuma(&pal, opts.from, opts.to, opts.luma, &opts.color);
		else
			adjustLuma(&pal, opts.from, opts.to, opts.luma);
	}

	if (opts.add)
		addColor(&pal, opts.from, opts.to, &opts.color, opts.prorate);
	else if (opts.sub)
		subColor(&pal, opts.from, opts.to, &opts.color, opts.prorate);
	else if (opts.blend)
		blendToColor(&pal, opts.from, opts.to, opts.blend, &opts.color,
				opts.prorate);

	ret = writePalette(&pal, opts.outfile);

	return ret;
}
Example #3
0
void tileReference::load(fstream& file){
    string line;
    string lineHdr;
    string lineTail;

    getline(file, line);
    while(line != "<endTileReference>"){
        size_t found = line.find_first_of(">");
        if(found!=string::npos){
            lineHdr = line.substr(0, found + 1);
            lineTail = line.substr(found + 1);

            if(lineHdr == "<id>"){
                readID(lineTail, false);
            }
            else if(lineHdr == "<palette>"){
                readPalette(lineTail);
            }
        }
        getline(file, line);
    }
}
Example #4
0
WINBITMAPINFOHEADER * PLBmpDecoder::getInfoHeader
    ( PLDataSource * pDataSrc,
      PLPixel32* pPal
    )
    // Decodes the bitmap file & info headers
{
  WINBITMAPFILEHEADER BFH;
  WINBITMAPINFOHEADER * pBMI;
  int offset = 0;

  BFH.bfType = ReadIWord (pDataSrc);

  while (BFH.bfType == 0x4142)
  {
    BFH.bfSize = ReadILong (pDataSrc);
    BFH.bfReserved1 = ReadIWord (pDataSrc);
    BFH.bfReserved2 = ReadIWord (pDataSrc);
    BFH.bfOffBits = ReadILong (pDataSrc);
    BFH.bfType = ReadIWord (pDataSrc);
    offset += 14;
  }

  BFH.bfSize = ReadILong (pDataSrc);
  BFH.bfReserved1 = ReadIWord (pDataSrc);
  BFH.bfReserved2 = ReadIWord (pDataSrc);
  BFH.bfOffBits = ReadILong (pDataSrc);

  // Check for bitmap file signature: First 2 bytes are 'BA', 'BM',
  // 'IC', 'PI', 'CI' or 'CP'
  if (!((BFH.bfType == 0x4142) || (BFH.bfType == 0x4d42) ||
        (BFH.bfType == 0x4349) || (BFH.bfType == 0x5043) ||
        (BFH.bfType == 0x4943) || (BFH.bfType == 0x5043)))
    raiseError (PL_ERRWRONG_SIGNATURE,
                "Bitmap decoder: This isn't a bitmap.");

  Trace (2, "Bitmap file signature found\n");

  pBMI = new WINBITMAPINFOHEADER;

  try
  {
    int Colors = 0;

    pBMI->biSize = ReadILong (pDataSrc);

    if (pBMI->biSize == 12)
    {
      // read as BITMAPCOREHEADER struct (OS2 1.x Format)
      pBMI->biWidth    = ReadIWord(pDataSrc);
      pBMI->biHeight   = ReadIWord(pDataSrc);
      pBMI->biPlanes   = ReadIWord(pDataSrc);
      pBMI->biBitCount = ReadIWord(pDataSrc);

      pBMI->biCompression = 0;
      pBMI->biSizeImage = 0;
      pBMI->biXPelsPerMeter = 0;
      pBMI->biYPelsPerMeter = 0;
      pBMI->biClrUsed = 0;
      pBMI->biClrImportant = 0;

      // Read palette if 8 bpp or less.
      if (pBMI->biBitCount <= 8)
      {
        Colors = readPalette (pBMI, pDataSrc, pPal, 3);
        pDataSrc->Skip(BFH.bfOffBits - 26 - 3 * Colors - offset);
      }
    }
    else if (pBMI->biSize == 40)
    {
      // read as BITMAPINFOHEADER struct
      pBMI->biWidth = ReadILong (pDataSrc);
      pBMI->biHeight = ReadILong (pDataSrc);
      pBMI->biPlanes = ReadIWord (pDataSrc);
      pBMI->biBitCount = ReadIWord (pDataSrc);
      pBMI->biCompression = ReadILong (pDataSrc);
      pBMI->biSizeImage = ReadILong (pDataSrc);
      pBMI->biXPelsPerMeter = ReadILong (pDataSrc);
      pBMI->biYPelsPerMeter = ReadILong (pDataSrc);
      pBMI->biClrUsed = ReadILong (pDataSrc);
      pBMI->biClrImportant = ReadILong (pDataSrc);

      // Read palette if 8 bpp or less.
      if (pBMI->biBitCount <= 8)
        Colors = readPalette (pBMI, pDataSrc, pPal, 4);
    }
    else
    {
      // read as BITMAPINFOHEADER struct (OS2 2.x Format)
      pBMI->biWidth = ReadILong (pDataSrc);
      pBMI->biHeight = ReadILong (pDataSrc);
      pBMI->biPlanes = ReadIWord (pDataSrc);
      pBMI->biBitCount = ReadIWord (pDataSrc);
      pBMI->biCompression = ReadILong (pDataSrc);
      pBMI->biSizeImage = ReadILong (pDataSrc);
      pBMI->biXPelsPerMeter = ReadILong (pDataSrc);
      pBMI->biYPelsPerMeter = ReadILong (pDataSrc);
      pBMI->biClrUsed = ReadILong (pDataSrc);
      pBMI->biClrImportant = ReadILong (pDataSrc);
      pDataSrc->Skip(pBMI->biSize - sizeof (WINBITMAPINFOHEADER));

      // Read palette if 8 bpp or less.
      if (pBMI->biBitCount <= 8)
        Colors = readPalette (pBMI, pDataSrc, pPal, 3);
    }

    Trace (2, "Bitmap header is ok.\n");

  }
  catch (PLTextException)
  {
    if (pBMI)
      delete pBMI;
    throw;
  }
  catch(...)
  {
    delete pBMI;
    throw;
  }
  return pBMI;
}
static
bool
PNGdecodeImage_real(imageSrcPtr src, imageDstPtr dst, 
		    long *paletteData, unsigned char *transData) {

    bool       OK = TRUE;
    bool saw_IDAT = FALSE,
             saw_PLTE = FALSE,
             saw_tRNS = FALSE;
    pngData      data;

    unsigned long chunkType = UNDF_CHUNK;
    long chunkLength;
    unsigned long CRC;

    if (!signatureOK(src)) {
        goto formaterror;               /* not a PNG image */
    }

    memset(&data, 0, sizeof(data));

    while (getChunk(src, &chunkType, &chunkLength)) {
        CRC = init_CRC(chunkType);

        if (chunkType == IHDR_CHUNK) {
            /* size of header is known.  headerOK => IHDR_CHUNK already seen */
            if ((chunkLength < 13) || headerOK(&data)) {
                goto formaterror;
            }

            CRC = readHeader(src, chunkLength, &data, CRC);

            if (!headerOK(&data)) {
                goto formaterror;
            }

            dst->setSize(dst, data.width, data.height);
            if ((data.colorType & CT_COLOR) == 0) {
                /* grayscale--set up a palette */
                int n = 0;
                long _p[4];

                switch (data.depth) {
                case 1:
                    _p[0] = 0;
                    _p[1] = 0xffffff;
                    n = 2;
                    dst->setColormap(dst, _p, n);
                    break;

                case 2:
                    _p[0] = 0;
                    _p[1] = 0x555555;
                    _p[2] = 0xaaaaaa;
                    _p[3] = 0xffffff;
                    n = 4;
                    dst->setColormap(dst, _p, n);
                    break;

                case 4:
                    {
                    long p[16];

                    for (n = 0; n < 16; ++n) {
                        p[n] = n*0x111111;
                    }

                    dst->setColormap(dst, p, n);
                    }
                    break;

                default:
                    {
                    long p[256];

                    for (n = 0; n < 256; ++n) {
                        p[n] = (n << 16) | (n << 8) | n;
                    }

                    dst->setColormap(dst, p, n);
                    }
                    break;
                }
            }
        } else if (chunkType == PLTE_CHUNK) {

            if ((chunkLength < 0) || (chunkLength > (256 * 3)) ||
                    ((chunkLength % 3) != 0)) {
                goto formaterror;
            }

            if ((data.paletteLength > 0) || saw_IDAT || saw_tRNS) {
                goto formaterror;
            }

            CRC = readPalette(src, chunkLength, &data, paletteData, CRC);
            if (data.palette == NULL) {
                goto formaterror;
            }

            if (data.colorType & CT_PALETTE) {
                dst->setColormap(dst, data.palette, data.paletteLength);
            }

            saw_PLTE = TRUE;
        } else if (chunkType == tRNS_CHUNK) {

            if (saw_IDAT || data.colorType == 4 
                         || data.colorType == 6) {
                goto formaterror;
            }

            CRC = readTransPal(src, chunkLength, &data, transData, CRC);
            if (data.trans == NULL) {
                goto formaterror;
            }

            dst->setTransMap(dst, data.trans, data.transLength,
                             saw_PLTE ? data.paletteLength : -1);

            saw_tRNS = TRUE;
        } else if (chunkType == IDAT_CHUNK) {
            idatInflateData  _data;
            FileObj fileObj;
            HeapManObj heapManObj;

            int compLen = 0;
            int decompLen = data.scanlength * data.height;
            unsigned char *decompBuf;
            long startPos = 0, lastGoodPos = 0;

            saw_IDAT = TRUE;

            if (data.interlace) {
                int i;

                /* decompLen is harder to calculate--there are extra rows! */
                decompLen = 0;
                for (i = 0; i < 7; ++i) {
                    int off    = 7 >> (i/2);            /* 7 7 3 3 1 1 0 */
                    int shift  = 3 - (((i - 1) / 2));   /* 3 3 3 2 2 1 1 */
                    int height = ((data.height + off) >> shift);
                    int width  = findPassWidth(i, &data);

                    data.lineBytes[i] = width;
                    data.passSize[i]  = width * height;

                    decompLen += data.passSize[i];
                }
            } else {