File: fxezquantize.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites:
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (115 lines) | stat: -rw-r--r-- 3,988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/********************************************************************************
*                                                                               *
*                   E Z   C o l o r   Q u a n t i z a t i o n                   *
*                                                                               *
*********************************************************************************
* Copyright (C) 1999,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"


/*
  Notes:

  - Use fxezquantize for a quick test to see if the image contains
    less than 256 colors; for example loading then saving back out
    an 8-bit GIF image.  This ensures that the original set of
    colors is maintained.
*/


using namespace FX;


/*******************************************************************************/

namespace FX {


extern FXbool fxezquantize(FXuchar* dst,const FXColor* src,FXColor* colormap,FXint& actualcolors,FXint w,FXint h,FXint maxcolors);


// EZ quantization may be used if w*h<=maxcolors, or if the actual colors
// used is less than maxcolors; using fxezquantize assures that no
// loss of data occurs repeatedly loading and saving the same file!
FXbool fxezquantize(FXuchar* dst,const FXColor* src,FXColor* colormap,FXint& actualcolors,FXint w,FXint h,FXint maxcolors){
  FXint   npixels=w*h;
  FXint   ncolors=0;
  FXColor color;
  FXint   i,p,x;
  FXColor  colortable[337];             // Colors encountered in image
  FXushort mapindex[337];               // Map index assigned to color

  FXASSERT(maxcolors<=256);

  // Clear map index
  memset(mapindex,0xff,sizeof(mapindex));

  // Hash all colors from image
  for(i=0; i<npixels; i++){

    // Get pixel
    color=src[i];

    // Find position in table
    p=color%337;
    x=color%331+1;
    while(mapindex[p]!=0xffff){         // Empty slot
      if(colortable[p]==color) goto nxt;
      p=(p+x)%337;
      }

    // If no more room in colormap, we failed
    if(ncolors>=maxcolors) return false;

    // Add new color
    colortable[p]=color;                // Add color to color hash table
    colormap[ncolors]=color;            // Add color to color map
    mapindex[p]=ncolors;                // Remember map index of this color
    ncolors++;

    // Next pixel
nxt:continue;
    }

  // Now loop through image, assigning map indices; all colors
  // must be in the map, so each lookup will be successful.
  for(i=0; i<npixels; i++){

    // Get pixel
    color=src[i];

    // Find position in table
    p=color%337;
    x=color%331+1;
    while(colortable[p]!=color){
      p=(p+x)%337;
      }

    // Output map index
    dst[i]=(FXuchar)mapindex[p];
    }

  // Actual number of colors used
  actualcolors=ncolors;

  return true;
  }

}