+/*
+
+Date: Wed, 05 Jan 2000 11:45:40 +0100
+From: Frits Boel <boel@niob.knaw.nl>
+To: julian.smart@ukonline.co.uk
+Subject: Patch for Motif ConvertToBitmap
+
+Hi Julian,
+
+I've been working on a wxWin application for image processing. From the
+beginning, I was surprised by the (lack of) speed of ConvertToBitmap,
+till I looked in the source code of image.cpp. I saw that converting a
+wxImage to a bitmap with 8-bit pixels is done with comparing every pixel
+to the 256 colors of the palet. A very time-consuming piece of code!
+
+Because I wanted a faster application, I've made a 'patch' for this. In
+short: every pixel of the image is compared to a sorted list with
+colors. If the color is found in the list, the palette entry is
+returned; if the color is not found, the color palette is searched and
+then the palette entry is returned and the color added to the sorted
+list.
+
+Maybe there is another method for this, namely changing the palette
+itself (if the colors are known, as is the case with tiffs with a
+colormap). I did not look at this, maybe someone else did?
+
+The code of the patch is attached, have a look on it, and maybe you will
+ship it with the next release of wxMotif?
+
+Regards,
+
+Frits Boel
+Software engineer at Hubrecht Laboratory, The Netherlands.
+
+*/
+
+class wxSearchColor
+{
+public:
+ wxSearchColor( void );
+ wxSearchColor( int size, XColor *colors );
+ ~wxSearchColor( void );
+
+ int SearchColor( int r, int g, int b );
+private:
+ int AddColor( unsigned int value, int pos );
+
+ int size;
+ XColor *colors;
+ unsigned int *color;
+ int *entry;
+
+ int bottom;
+ int top;
+};
+
+wxSearchColor::wxSearchColor( void )
+{
+ size = 0;
+ colors = (XColor*) NULL;
+ color = (unsigned int *) NULL;
+ entry = (int*) NULL;
+
+ bottom = 0;
+ top = 0;
+}
+
+wxSearchColor::wxSearchColor( int size_, XColor *colors_ )
+{
+ int i;
+ size = size_;
+ colors = colors_;
+ color = new unsigned int[size];
+ entry = new int [size];
+
+ for (i = 0; i < size; i++ ) {
+ entry[i] = -1;
+ }
+
+ bottom = top = ( size >> 1 );
+}
+
+wxSearchColor::~wxSearchColor( void )
+{
+ if ( color ) delete color;
+ if ( entry ) delete entry;
+}
+
+int wxSearchColor::SearchColor( int r, int g, int b )
+{
+ unsigned int value = ( ( ( r * 256 ) + g ) * 256 ) + b;
+ int begin = bottom;
+ int end = top;
+ int middle;
+
+ while ( begin <= end ) {
+
+ middle = ( begin + end ) >> 1;
+
+ if ( value == color[middle] ) {
+ return( entry[middle] );
+ } else if ( value < color[middle] ) {
+ end = middle - 1;
+ } else {
+ begin = middle + 1;
+ }
+
+ }
+
+ return AddColor( value, middle );
+}
+
+int wxSearchColor::AddColor( unsigned int value, int pos )
+{
+ int i;
+ int pixel = -1;
+ int max = 3 * (65536);
+ for ( i = 0; i < 256; i++ ) {
+ int rdiff = ((value >> 8) & 0xFF00 ) - colors[i].red;
+ int gdiff = ((value ) & 0xFF00 ) - colors[i].green;
+ int bdiff = ((value << 8) & 0xFF00 ) - colors[i].blue;
+ int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
+ if (sum < max) { pixel = i; max = sum; }
+ }
+
+ if ( entry[pos] < 0 ) {
+ color[pos] = value;
+ entry[pos] = pixel;
+ } else if ( value < color[pos] ) {
+
+ if ( bottom > 0 ) {
+ for ( i = bottom; i < pos; i++ ) {
+ color[i-1] = color[i];
+ entry[i-1] = entry[i];
+ }
+ bottom--;
+ color[pos-1] = value;
+ entry[pos-1] = pixel;
+ } else if ( top < size-1 ) {
+ for ( i = top; i >= pos; i-- ) {
+ color[i+1] = color[i];
+ entry[i+1] = entry[i];
+ }
+ top++;
+ color[pos] = value;
+ entry[pos] = pixel;
+ }
+
+ } else {
+
+ if ( top < size-1 ) {
+ for ( i = top; i > pos; i-- ) {
+ color[i+1] = color[i];
+ entry[i+1] = entry[i];
+ }
+ top++;
+ color[pos+1] = value;
+ entry[pos+1] = pixel;
+ } else if ( bottom > 0 ) {
+ for ( i = bottom; i < pos; i++ ) {
+ color[i-1] = color[i];
+ entry[i-1] = entry[i];
+ }
+ bottom--;
+ color[pos] = value;
+ entry[pos] = pixel;
+ }
+
+ }
+
+ return( pixel );
+}
+