示例#1
0
void BmpImage::toGrayscale()
{
	if (bitsPerPixel != 8 && bitsPerPixel != 24)
		return;

	int w = rowBytes();
	int avg;

	if (bitsPerPixel == 24)
	{
		ByteBuffer* bmpBuffer = new ByteBuffer(bitmapSize, bmpBits);
		BinaryReader* reader = new BinaryReader(bmpBuffer);

		byte blue, green, red;

		for (unsigned int i = 0; i < height; i++)
		{
			for (unsigned int j = 0; j < width; j++)
			{
				blue = reader->readByte(i*w+j*3);
				green = reader->readByte(i*w+j*3+1);
				red = reader->readByte(i*w+j*3+2);

				avg = (red + green + blue)/3;
				avg = (avg << 16) | (avg << 8) | (avg);

				bmpBuffer->seek(i*w+j*3);
				bmpBuffer->addRGB(avg);
			}
		}

		bmpBits = bmpBuffer->getBytes();
		return;
	}

	int colorTableIndex;

	for (unsigned int i = 0; i < height; i++)
	{
		for (unsigned int j = 0; j < width; j++)
		{
			colorTableIndex = bmpBits[i*w+j];
			avg = (colorPalette[colorTableIndex] & 0x00FF0000) >> 16;
			avg += (colorPalette[colorTableIndex] & 0x0000FF00) >> 8;
			avg += (colorPalette[colorTableIndex] & 0x000000FF);
			avg /= 3;

			bmpBits[i*w+j] = avg;
		}
	}

	for (int k = 0; k < colorPaletteSize; k++)
	{
		colorPalette[k] = (k << 16) | (k << 8) | (k);
	}
}
示例#2
0
/**
 * BmpImage::serialize
 *	The method will create a byte stream out of the BMP image object, 
 *	stream that can be later written to file as raw bitmap image.
 */
ByteBuffer* BmpImage::serialize()
{
	if (fileType == 0)
		return NULL;
	ByteBuffer* img = new ByteBuffer(this->fileSize);
	
	// write the file header
	*img << fileType;
	*img << fileSize; 
	*img << Xhot;
	*img << Yhot;
	*img << bitmapOffset;

	// the bmp file header
	if (version == NOTBMP || version == BMPUNKNOWN)
		return NULL;

	// add the size of the bmp header
	*img << headerSize;

	if (version == BMPOLD)
	{
		// because of the unified treatment
		img->addWord((word) width);
		img->addWord((word) height);

		*img << (word) planes;
		*img << (word) bitsPerPixel;

		// add the palette
		for (int i = 0; i < colorPaletteSize; i++)
		{
			// suppose to add 3 byte rgb structures
			img->addRGB(colorPalette[i]);
		}

	}
	else
	{
		*img << width;
		*img << height;
		*img << planes;
		*img << bitsPerPixel;
		*img << compression;
		*img << bitmapSize;
		*img << (dword) horizResolution;
		*img << (dword) vertResolution;
		*img << colorsUsed;
		*img << colorsImportant;
		
		if (version == BMPOS2NEW) // OS2 header
		{
			// add up the rest of the data until 64 bytes
			*img << units;
			*img << reserved;
			*img << recording;
			*img << rendering;
			*img << size1;
			*img << size2;
			*img << identifier;
		}
		
		if (version == BMPNEWWINV4HDRSIZE)
		{
			// seek backwards and init to 0 everything
			img->seek(BMPFILEHDRSIZE + BMPNEWWINHDRSIZE);
			
			for (int i = BMPFILEHDRSIZE + BMPNEWWINHDRSIZE; i < BMPNEWWINV4HDRSIZE; i++)
			{
				*img << (byte) 0x00;
			}
		}

		// skip to the color table
		printf("\nposition : %d\n", img->getPosition());
		printf("length: %d\n", img->getLength());
		printf("seek to : %d\n", BMPFILEHDRSIZE + headerSize);
		img->seek(BMPFILEHDRSIZE + headerSize);

		for (int i = 0; i < colorPaletteSize; i++)
		{
			*img << colorPalette[i];
		}
	}

	img->seek(bitmapOffset);

	for (unsigned int i = 0; i < bitmapSize; i++)
	{
		*img << bmpBits[i];
	}

	//img->trimToSize();
	return img;
}