Example #1
0
bool CImageLoaderPSD::readRawImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const
{
	u8* tmpData = new u8[header.width * header.height];

	for (s32 channel=0; channel<header.channels && channel < 3; ++channel)
	{
		if (!file->read(tmpData, sizeof(c8) * header.width * header.height))
		{
			os::Printer::log("Error reading color channel\n", file->getFileName(), ELL_ERROR);
			break;
		}

		s16 shift = getShiftFromChannel((c8)channel, header);
		if (shift != -1)
		{
			u32 mask = 0xff << shift;

			for (u32 x=0; x<header.width; ++x)
			{
				for (u32 y=0; y<header.height; ++y)
				{
					s32 index = x + y*header.width;
					imageData[index] = ~(~imageData[index] | mask);
					imageData[index] |= tmpData[index] << shift;
				}
			}
		}

	}

	delete [] tmpData;
	return true;
}
Example #2
0
bool CImageLoaderPSD::readRawImageData(io::IReadFile* file)
{
    u8* tmpData = new u8[header.width * header.height];

    for (s32 channel=0; channel<header.channels && channel < 3; ++channel)
    {
        if (!file->read(tmpData, sizeof(c8) * header.width * header.height))
        {
            LOGGER.log("Error reading color channel ", file->getFileName(), io::ELL_ERROR);
            break;
        }

        c8 shift = getShiftFromChannel(channel);
        if (shift != -1)
        {
            u32 mask = 0xff << shift;

            for (u32 x=0; x<header.width; ++x)
                for (u32 y=0; y<header.height; ++y)
                {
                    s32 index = x + y*header.width;
                    imageData[index] = ~(~imageData[index] | mask);
                    imageData[index] |= tmpData[index] << shift;
                }
        }

    }
        
    delete [] tmpData;
    return true;
}
Example #3
0
bool CImageLoaderPSD::readRLEImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const
{
	/*	If the compression code is 1, the image data
		starts with the byte counts for all the scan lines in the channel
		(LayerBottom LayerTop), with each count stored as a two
		byte value. The RLE compressed data follows, with each scan line
		compressed separately. The RLE compression is the same compres-sion
		algorithm used by the Macintosh ROM routine PackBits, and
		the TIFF standard.
		If the Layer's Size, and therefore the data, is odd, a pad byte will
		be inserted at the end of the row.
	*/

	/*
	A pseudo code fragment to unpack might look like this:

	Loop until you get the number of unpacked bytes you are expecting:
		Read the next source byte into n.
		If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
		Else if n is between -127 and -1 inclusive, copy the next byte -n+1
		times.
		Else if n is -128, noop.
	Endloop

	In the inverse routine, it is best to encode a 2-byte repeat run as a replicate run
	except when preceded and followed by a literal run. In that case, it is best to merge
	the three runs into one literal run. Always encode 3-byte repeats as replicate runs.
	That is the essence of the algorithm. Here are some additional rules:
	- Pack each row separately. Do not compress across row boundaries.
	- The number of uncompressed bytes per row is defined to be (ImageWidth + 7)
	/ 8. If the uncompressed bitmap is required to have an even number of bytes per
	row, decompress into word-aligned buffers.
	- If a run is larger than 128 bytes, encode the remainder of the run as one or more
	additional replicate runs.
	When PackBits data is decompressed, the result should be interpreted as per com-pression
	type 1 (no compression).
	*/

	u8* tmpData = new u8[header.width * header.height];
	u16 *rleCount= new u16 [header.height * header.channels];

	s32 size=0;

	for (u32 y=0; y<header.height * header.channels; ++y)
	{
		if (!file->read(&rleCount[y], sizeof(u16)))
		{
			delete [] tmpData;
			delete [] rleCount;
			os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);
			return false;
		}

#ifndef __BIG_ENDIAN__
		rleCount[y] = os::Byteswap::byteswap(rleCount[y]);
#endif
		size += rleCount[y];
	}

	s8 *buf = new s8[size];
	if (!file->read(buf, size))
	{
		delete [] rleCount;
		delete [] buf;
		delete [] tmpData;
		os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);
		return false;
	}

	u16 *rcount=rleCount;

	s8 rh;
	u16 bytesRead;
	u8 *dest;
	s8 *pBuf = buf;

	// decompress packbit rle

	for (s32 channel=0; channel<header.channels; channel++)
	{
		for (u32 y=0; y<header.height; ++y, ++rcount)
		{
			bytesRead=0;
			dest = &tmpData[y*header.width];

			while (bytesRead < *rcount)
			{
				rh = *pBuf++;
				++bytesRead;

				if (rh >= 0)
				{
					++rh;

					while (rh--)
					{
						*dest = *pBuf++;
						++bytesRead;
						++dest;
					}
				}
				else
				if (rh > -128)
				{
					rh = -rh +1;

					while (rh--)
					{
						*dest = *pBuf;
						++dest;
					}

					++pBuf;
					++bytesRead;
				}
			}
		}

		s16 shift = getShiftFromChannel((c8)channel, header);

		if (shift != -1)
		{
			u32 mask = 0xff << shift;

			for (u32 x=0; x<header.width; ++x)
				for (u32 y=0; y<header.height; ++y)
				{
					s32 index = x + y*header.width;
					imageData[index] = ~(~imageData[index] | mask);
					imageData[index] |= tmpData[index] << shift;
				}
		}
	}

	delete [] rleCount;
	delete [] buf;
	delete [] tmpData;

	return true;
}