Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/FreeImage/LFPQuantizer.cpp  view on Meta::CPAN

// - Carsten Klein (cklein05@users.sourceforge.net)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================

#include "Quantizers.h"
#include "FreeImage.h"
#include "Utilities.h"

LFPQuantizer::LFPQuantizer(unsigned PaletteSize) :
		m_size(0), m_limit(PaletteSize), m_index(0) {
	m_map = new MapEntry[MAP_SIZE];
	memset(m_map, 0xFF, MAP_SIZE * sizeof(MapEntry));
}

LFPQuantizer::~LFPQuantizer() {
    delete[] m_map;
}

FIBITMAP* LFPQuantizer::Quantize(FIBITMAP *dib, int ReserveSize, RGBQUAD *ReservePalette) {

	if (ReserveSize > 0 && ReservePalette != NULL) {
		AddReservePalette(ReservePalette, ReserveSize);
	}

	const unsigned width = FreeImage_GetWidth(dib);
	const unsigned height = FreeImage_GetHeight(dib);

	FIBITMAP *dib8 = FreeImage_Allocate(width, height, 8);
	if (dib8 == NULL) {
		return NULL;
	}

	const unsigned src_pitch = FreeImage_GetPitch(dib);
	const unsigned dst_pitch = FreeImage_GetPitch(dib8);

	const BYTE * const src_bits = FreeImage_GetBits(dib);
	BYTE * const dst_bits = FreeImage_GetBits(dib8);

	unsigned last_color = -1;
	int last_index = 0;

	if (FreeImage_GetBPP(dib) == 24) {

		// Getting the source pixel as an unsigned int is much faster than
		// working with FI_RGBA_xxx and shifting. However, this may fail
		// for the very last pixel, since its rgbReserved member (alpha)
		// may actually point to an address beyond the bitmap's memory. So,
		// we do not process the last scanline in the first loop.

		// Process all but the last scanline.
		for (unsigned y = 0; y < height - 1; ++y) {
			BYTE *dst_line = dst_bits + y * dst_pitch;
			const BYTE *src_line = src_bits + y * src_pitch;
			for (unsigned x = 0; x < width; ++x) {
				const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
				if (color != last_color) {
					last_color = color;
					last_index = GetIndexForColor(color);
					if (last_index == -1) {
						FreeImage_Unload(dib8);
						return NULL;
					}
				}
				dst_line[x] = last_index;
				src_line += 3;
			}
		}

		// Process all but the last pixel of the last scanline.
		BYTE *dst_line = dst_bits + (height - 1) * dst_pitch;
		const BYTE *src_line = src_bits + (height - 1) * src_pitch;
		for (unsigned x = 0; x < width - 1; ++x) {
			const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
			if (color != last_color) {
				last_color = color;
				last_index = GetIndexForColor(color);
				if (last_index == -1) {
					FreeImage_Unload(dib8);
					return NULL;
				}
			}
			dst_line[x] = last_index;
			src_line += 3;
		}

		// Process the last pixel (src_line should already point to it).
		const unsigned color = 0 | src_line[FI_RGBA_BLUE] << FI_RGBA_BLUE_SHIFT
				| src_line[FI_RGBA_GREEN] << FI_RGBA_GREEN_SHIFT
				| src_line[FI_RGBA_RED] << FI_RGBA_RED_SHIFT;
		if (color != last_color) {
			last_color = color;
			last_index = GetIndexForColor(color);
			if (last_index == -1) {
				FreeImage_Unload(dib8);
				return NULL;
			}
		}
		dst_line[width - 1] = last_index;

	} else {
		for (unsigned y = 0; y < height; ++y) {
			BYTE *dst_line = dst_bits + y * dst_pitch;
			const BYTE *src_line = src_bits + y * src_pitch;
			for (unsigned x = 0; x < width; ++x) {
				const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
				if (color != last_color) {
					last_color = color;
					last_index = GetIndexForColor(color);



( run in 0.852 second using v1.01-cache-2.11-cpan-ceb78f64989 )