kviewshell
DjVuPalette.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef _DJVUPALETTE_H_
00058 #define _DJVUPALETTE_H_
00059 #ifdef HAVE_CONFIG_H
00060 #include "config.h"
00061 #endif
00062 #if NEED_GNUG_PRAGMAS
00063 # pragma interface
00064 #endif
00065
00066
00067 #include "GContainer.h"
00068 #include "GPixmap.h"
00069 #include <string.h>
00070
00071
00072 #ifdef HAVE_NAMESPACES
00073 namespace DJVU {
00074 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00075 }
00076 #endif
00077 #endif
00078
00079
00091
00092
00116 #ifdef _WIN32_WCE_EMULATION // Work around odd behavior under WCE Emulation
00117 #define CALLINGCONVENTION __cdecl
00118 #else
00119 #define CALLINGCONVENTION
00120 #endif
00121
00122 class DjVuPalette : public GPEnabled
00123 {
00124 protected:
00125 DjVuPalette(void);
00126 public:
00128 static GP<DjVuPalette> create(void) {return new DjVuPalette();}
00129
00131 ~DjVuPalette();
00132
00133 DjVuPalette(const DjVuPalette &ref);
00134 DjVuPalette& operator=(const DjVuPalette &ref);
00135
00137 void histogram_clear();
00140 void histogram_add(const GPixel &p, int weight);
00143 void histogram_add(const unsigned char *bgr, int weight);
00150 void histogram_norm_and_add(const int *bgr, int weight);
00156 int compute_palette(int maxcolors, int minboxsize=0);
00160 int compute_pixmap_palette(const GPixmap &pm, int ncolors, int minboxsize=0);
00161
00163 int size() const;
00165 int color_to_index(const GPixel &p);
00167 int color_to_index(const unsigned char *bgr);
00169 void index_to_color(int index, GPixel &p) const;
00172 void index_to_color(int index, unsigned char *bgr) const;
00175 void quantize(GPixmap &pm);
00177 int compute_palette_and_quantize(GPixmap &pm, int maxcolors, int minboxsize=0);
00178
00183 void color_correct(double corr);
00184
00188 GTArray<short> colordata;
00192 void get_color(int nth, GPixel &out) const;
00193
00196 void encode_rgb_entries(ByteStream &bs) const;
00199 void decode_rgb_entries(ByteStream &bs, const int palettesize);
00204 void encode(GP<ByteStream> bs) const;
00209 void decode(GP<ByteStream> bs);
00210
00211 private:
00212
00213 int mask;
00214 GMap<int,int> *hist;
00215
00216 struct PColor { unsigned char p[4]; };
00217 GTArray<PColor> palette;
00218 GMap<int,int> *pmap;
00219
00220 void allocate_hist();
00221 void allocate_pmap();
00222 static int CALLINGCONVENTION bcomp (const void*, const void*);
00223 static int CALLINGCONVENTION gcomp (const void*, const void*);
00224 static int CALLINGCONVENTION rcomp (const void*, const void*);
00225 static int CALLINGCONVENTION lcomp (const void*, const void*);
00226 int color_to_index_slow(const unsigned char *bgr);
00227 private:
00228 static void encode(ByteStream *);
00229 static void decode(ByteStream *);
00230 };
00231
00232
00234
00235
00236
00237
00238 inline void
00239 DjVuPalette::histogram_clear()
00240 {
00241 delete hist;
00242 hist = 0;
00243 mask = 0;
00244 }
00245
00246 inline void
00247 DjVuPalette::histogram_add(const unsigned char *bgr, int weight)
00248 {
00249 if (weight > 0)
00250 {
00251 if (!hist || hist->size()>=0x4000)
00252 allocate_hist();
00253 int key = (bgr[0]<<16)|(bgr[1]<<8)|(bgr[2])|(mask);
00254 (*hist)[key] += weight;
00255 }
00256 }
00257
00258 inline void
00259 DjVuPalette::histogram_add(const GPixel &p, int weight)
00260 {
00261 histogram_add(&p.b, weight);
00262 }
00263
00264 inline void
00265 DjVuPalette::histogram_norm_and_add(const int *bgr, int weight)
00266 {
00267 if (weight > 0)
00268 {
00269 int p0 = bgr[0]/weight; if (p0>255) p0=255;
00270 int p1 = bgr[1]/weight; if (p1>255) p1=255;
00271 int p2 = bgr[2]/weight; if (p2>255) p2=255;
00272 if (!hist || hist->size()>=0x4000)
00273 allocate_hist();
00274 int key = (p0<<16)|(p1<<8)|(p2)|(mask);
00275 (*hist)[key] += weight;
00276 }
00277 }
00278
00279 inline int
00280 DjVuPalette::size() const
00281 {
00282 return palette.size();
00283 }
00284
00285 inline int
00286 DjVuPalette::color_to_index(const unsigned char *bgr)
00287 {
00288 if (! pmap)
00289 allocate_pmap();
00290 int key = (bgr[0]<<16)|(bgr[1]<<8)|(bgr[2]);
00291 GPosition p = pmap->contains(key);
00292 if ( p)
00293 return (*pmap)[p];
00294 return color_to_index_slow(bgr);
00295 }
00296
00297 inline int
00298 DjVuPalette::color_to_index(const GPixel &p)
00299 {
00300 return color_to_index(&p.b);
00301 }
00302
00303 inline void
00304 DjVuPalette::index_to_color(int index, unsigned char *bgr) const
00305 {
00306 const PColor &color = palette[index];
00307 bgr[0] = color.p[0];
00308 bgr[1] = color.p[1];
00309 bgr[2] = color.p[2];
00310 }
00311
00312 inline void
00313 DjVuPalette::index_to_color(int index, GPixel &p) const
00314 {
00315 index_to_color(index, &p.b);
00316 }
00317
00318 inline void
00319 DjVuPalette::get_color(int nth, GPixel &p) const
00320 {
00321 index_to_color(colordata[nth], p);
00322 }
00323
00324
00325
00326
00327
00328 #ifdef HAVE_NAMESPACES
00329 }
00330 # ifndef NOT_USING_DJVU_NAMESPACE
00331 using namespace DJVU;
00332 # endif
00333 #endif
00334 #endif
00335
00336
00337
00338
00339