Beispiel #1
0
static status_t
convert_data_to_bits(pcx_header &header, StreamBuffer &source,
	BPositionIO &target)
{
	uint16 bitsPerPixel = header.bitsPerPixel;
	uint16 bytesPerLine = header.bytesPerLine;
	uint32 width = header.xMax - header.xMin + 1;
	uint32 height = header.yMax - header.yMin + 1;
	uint16 numPlanes = header.numPlanes;
	uint32 scanLineLength = numPlanes * bytesPerLine;

	// allocate buffers
	TempAllocator scanLineAllocator;
	TempAllocator paletteAllocator;
	uint8 *scanLineData[height];
	uint8 *palette = (uint8 *)paletteAllocator.Allocate(3 * 256);
	status_t status = B_OK;

	for (uint32 row = 0; row < height; row++) {
		TRACE("scanline %ld\n", row);
		scanLineData[row] = (uint8 *)scanLineAllocator.Allocate(scanLineLength);
		if (scanLineData[row] == NULL)
			return B_NO_MEMORY;
		uint8 *line = scanLineData[row];
		uint32 index = 0;
		uint8 x;
		do {
			if (source.Read(&x, 1) != 1) {
				status = B_IO_ERROR;
				break;
			}
			if ((x & 0xc0) == 0xc0) {
				uint32 count = x & 0x3f;
				if (index + count - 1 > scanLineLength) {
					status = B_BAD_DATA;
					break;
				}
				if (source.Read(&x, 1) != 1) {
					status = B_IO_ERROR;
					break;
				}
				for (uint32 i = 0; i < count; i++)
					line[index++] = x;
			} else {
				line[index++] = x;
			}
		} while (index < scanLineLength);

		if (status != B_OK) {
			// If we've already read more than a third of the file, display
			// what we have, ie. ignore the error.
			if (row < height / 3)
				return status;

			memset(scanLineData + row, 0, sizeof(uint8*) * (height - row));
			break;
		}
	}

	if (bitsPerPixel == 8 && numPlanes == 1) {
		TRACE("palette reading %p 8\n", palette);
		uint8 x;
		if (status != B_OK || source.Read(&x, 1) != 1 || x != 12) {
			// Try again by repositioning the file stream
			if (source.Seek(-3 * 256 - 1, SEEK_END) < 0)
				return B_BAD_DATA;
			if (source.Read(&x, 1) != 1)
				return B_IO_ERROR;
			if (x != 12)
				return B_BAD_DATA;
		}
		if (source.Read(palette, 256 * 3) != 256 * 3)
			return B_IO_ERROR;
	} else {
		TRACE("palette reading %p palette\n", palette);
		memcpy(palette, &header.paletteInfo, 48);
	}

	uint8 alpha = 255;
	if (bitsPerPixel == 1 && numPlanes == 1) {
		TRACE("target writing 1\n");
		palette[0] = palette[1] = palette[2] = 0;
		palette[3] = palette[4] = palette[5] = 0xff;
		for (uint32 row = 0; row < height; row++) {
			uint8 *line = scanLineData[row];
			if (line == NULL)
				break;
			uint8 mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
			for (uint32 i = 0; i < width; i++) {
				bool isBit = ((line[i >> 3] & mask[i & 7]) != 0) ? true : false;
				target.Write(&palette[!isBit ? 2 : 5], 1);
				target.Write(&palette[!isBit ? 1 : 4], 1);
				target.Write(&palette[!isBit ? 0 : 3], 1);
				target.Write(&alpha, 1);
			}
		}
	} else if (bitsPerPixel == 4 && numPlanes == 1) {
Beispiel #2
0
 void AsyncSend(StreamBuffer& buffer, _UInt32 len)
 {
     buffer.Read(m_Wrapper->m_OStream, len);
     AsyncSend();
 }