util/gencmap/gencmap.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the utils of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <qcolor.h>
       
    43 #include <stdio.h>
       
    44 #include <stdlib.h>
       
    45 #include <limits.h>
       
    46 
       
    47 
       
    48 #define APPLE_CMAP 1
       
    49 
       
    50 struct Col {
       
    51     int r,g,b;
       
    52 };
       
    53 
       
    54 #if SPACE_SEARCH
       
    55 #define MAPSIZE 256
       
    56 #define ACCURACY 4     // Bits-per-channel
       
    57 #define SPACESIZE ((1<<ACCURACY)*(1<<ACCURACY)*(1<<ACCURACY))
       
    58 #define R(c) (((c>>(8-ACCURACY)*2)&((1<<ACCURACY)-1))<<ACCURACY)
       
    59 #define G(c) (((c>>(8-ACCURACY))&((1<<ACCURACY)-1))<<ACCURACY)
       
    60 #define B(c) (((c>>0)&((1<<ACCURACY)-1))<<ACCURACY)
       
    61 #define COL(c) (((c.r>>(8-ACCURACY))<<8)|((c.b>>(8-ACCURACY))<<4)|(c.g>>(8-ACCURACY)))
       
    62 #elif APPLE_CMAP
       
    63 #define SPACESIZE 216
       
    64 #define MAPSIZE 216
       
    65 #define R(c) ((apple_cmap[c]>>16)&0xff)
       
    66 #define G(c) ((apple_cmap[c]>>8)&0xff)
       
    67 #define B(c) ((apple_cmap[c]>>0)&0xff)
       
    68 #define COL(c) findapple(c)
       
    69 static int apple_cmap[216] = {
       
    70 0xffffff,
       
    71 0xffffcc,
       
    72 0xffff99,
       
    73 0xffff66,
       
    74 0xffff33,
       
    75 0xffff00,
       
    76 0xffccff,
       
    77 0xffcccc,
       
    78 0xffcc99,
       
    79 0xffcc66,
       
    80 0xffcc33,
       
    81 0xffcc00,
       
    82 0xff99ff,
       
    83 0xff99cc,
       
    84 0xff9999,
       
    85 0xff9966,
       
    86 0xff9933,
       
    87 0xff9900,
       
    88 0xff66ff,
       
    89 0xff66cc,
       
    90 0xff6699,
       
    91 0xff6666,
       
    92 0xff6633,
       
    93 0xff6600,
       
    94 0xff33ff,
       
    95 0xff33cc,
       
    96 0xff3399,
       
    97 0xff3366,
       
    98 0xff3333,
       
    99 0xff3300,
       
   100 0xff00ff,
       
   101 0xff00cc,
       
   102 0xff0099,
       
   103 0xff0066,
       
   104 0xff0033,
       
   105 0xff0000,
       
   106 0xccffff,
       
   107 0xccffcc,
       
   108 0xccff99,
       
   109 0xccff66,
       
   110 0xccff33,
       
   111 0xccff00,
       
   112 0xccccff,
       
   113 0xcccccc,
       
   114 0xcccc99,
       
   115 0xcccc66,
       
   116 0xcccc33,
       
   117 0xcccc00,
       
   118 0xcc99ff,
       
   119 0xcc99cc,
       
   120 0xcc9999,
       
   121 0xcc9966,
       
   122 0xcc9933,
       
   123 0xcc9900,
       
   124 0xcc66ff,
       
   125 0xcc66cc,
       
   126 0xcc6699,
       
   127 0xcc6666,
       
   128 0xcc6633,
       
   129 0xcc6600,
       
   130 0xcc33ff,
       
   131 0xcc33cc,
       
   132 0xcc3399,
       
   133 0xcc3366,
       
   134 0xcc3333,
       
   135 0xcc3300,
       
   136 0xcc00ff,
       
   137 0xcc00cc,
       
   138 0xcc0099,
       
   139 0xcc0066,
       
   140 0xcc0033,
       
   141 0xcc0000,
       
   142 0x99ffff,
       
   143 0x99ffcc,
       
   144 0x99ff99,
       
   145 0x99ff66,
       
   146 0x99ff33,
       
   147 0x99ff00,
       
   148 0x99ccff,
       
   149 0x99cccc,
       
   150 0x99cc99,
       
   151 0x99cc66,
       
   152 0x99cc33,
       
   153 0x99cc00,
       
   154 0x9999ff,
       
   155 0x9999cc,
       
   156 0x999999,
       
   157 0x999966,
       
   158 0x999933,
       
   159 0x999900,
       
   160 0x9966ff,
       
   161 0x9966cc,
       
   162 0x996699,
       
   163 0x996666,
       
   164 0x996633,
       
   165 0x996600,
       
   166 0x9933ff,
       
   167 0x9933cc,
       
   168 0x993399,
       
   169 0x993366,
       
   170 0x993333,
       
   171 0x993300,
       
   172 0x9900ff,
       
   173 0x9900cc,
       
   174 0x990099,
       
   175 0x990066,
       
   176 0x990033,
       
   177 0x990000,
       
   178 0x66ffff,
       
   179 0x66ffcc,
       
   180 0x66ff99,
       
   181 0x66ff66,
       
   182 0x66ff33,
       
   183 0x66ff00,
       
   184 0x66ccff,
       
   185 0x66cccc,
       
   186 0x66cc99,
       
   187 0x66cc66,
       
   188 0x66cc33,
       
   189 0x66cc00,
       
   190 0x6699ff,
       
   191 0x6699cc,
       
   192 0x669999,
       
   193 0x669966,
       
   194 0x669933,
       
   195 0x669900,
       
   196 0x6666ff,
       
   197 0x6666cc,
       
   198 0x666699,
       
   199 0x666666,
       
   200 0x666633,
       
   201 0x666600,
       
   202 0x6633ff,
       
   203 0x6633cc,
       
   204 0x663399,
       
   205 0x663366,
       
   206 0x663333,
       
   207 0x663300,
       
   208 0x6600ff,
       
   209 0x6600cc,
       
   210 0x660099,
       
   211 0x660066,
       
   212 0x660033,
       
   213 0x660000,
       
   214 0x33ffff,
       
   215 0x33ffcc,
       
   216 0x33ff99,
       
   217 0x33ff66,
       
   218 0x33ff33,
       
   219 0x33ff00,
       
   220 0x33ccff,
       
   221 0x33cccc,
       
   222 0x33cc99,
       
   223 0x33cc66,
       
   224 0x33cc33,
       
   225 0x33cc00,
       
   226 0x3399ff,
       
   227 0x3399cc,
       
   228 0x339999,
       
   229 0x339966,
       
   230 0x339933,
       
   231 0x339900,
       
   232 0x3366ff,
       
   233 0x3366cc,
       
   234 0x336699,
       
   235 0x336666,
       
   236 0x336633,
       
   237 0x336600,
       
   238 0x3333ff,
       
   239 0x3333cc,
       
   240 0x333399,
       
   241 0x333366,
       
   242 0x333333,
       
   243 0x333300,
       
   244 0x3300ff,
       
   245 0x3300cc,
       
   246 0x330099,
       
   247 0x330066,
       
   248 0x330033,
       
   249 0x330000,
       
   250 0x00ffff,
       
   251 0x00ffcc,
       
   252 0x00ff99,
       
   253 0x00ff66,
       
   254 0x00ff33,
       
   255 0x00ff00,
       
   256 0x00ccff,
       
   257 0x00cccc,
       
   258 0x00cc99,
       
   259 0x00cc66,
       
   260 0x00cc33,
       
   261 0x00cc00,
       
   262 0x0099ff,
       
   263 0x0099cc,
       
   264 0x009999,
       
   265 0x009966,
       
   266 0x009933,
       
   267 0x009900,
       
   268 0x0066ff,
       
   269 0x0066cc,
       
   270 0x006699,
       
   271 0x006666,
       
   272 0x006633,
       
   273 0x006600,
       
   274 0x0033ff,
       
   275 0x0033cc,
       
   276 0x003399,
       
   277 0x003366,
       
   278 0x003333,
       
   279 0x003300,
       
   280 0x0000ff,
       
   281 0x0000cc,
       
   282 0x000099,
       
   283 0x000066,
       
   284 0x000033,
       
   285 0x000000,
       
   286 };
       
   287 int findapple(Col c)
       
   288 {
       
   289     for (int i=0; i<216; i++)
       
   290 	if (apple_cmap[i]==(c.r<<16)|(c.g<<8)|c.b) return i;
       
   291     abort();
       
   292 }
       
   293 #endif
       
   294 
       
   295 #define SQ(x) ((x)*(x))
       
   296 #define D(c1,c2) (SQ(R(c1)-R(c2))+SQ(G(c1)-G(c2))+SQ(B(c1)-B(c2)))
       
   297 
       
   298 main()
       
   299 {
       
   300     Col c[256] = {
       
   301 	{ 0,0,0 },
       
   302 	{ 255,255,255 },
       
   303 	{ 255,0,0 }, { 0,255,0 }, { 0,0,255 },
       
   304 	{ 255,255,0 }, { 0,255,255 }, { 255,0,255 },
       
   305 	#define PREALLOC 8
       
   306 	{ 96,96,96 }, { 192,192,192 },
       
   307 	//#define PREALLOC 10
       
   308     };
       
   309     int done[SPACESIZE];
       
   310     for (int a=0; a<SPACESIZE; a++) done[a]=0;
       
   311     for (int a=0; a<PREALLOC; a++) done[COL(c[a])]=1;
       
   312 
       
   313     for (int allocated=PREALLOC; allocated<MAPSIZE; allocated++) {
       
   314 	int mostdist;
       
   315 	int dist=0;
       
   316 	for (int a=0; a<SPACESIZE; a++) {
       
   317 	    if (!done[a]) {
       
   318 		int closeness=INT_MAX;
       
   319 		for (int b=0; b<SPACESIZE; b++) {
       
   320 		    if (done[b]) {
       
   321 			int d=D(a,b);
       
   322 			if (d < closeness) {
       
   323 			    closeness=d;
       
   324 			}
       
   325 		    }
       
   326 		}
       
   327 		if (closeness > dist) {
       
   328 		    mostdist=a;
       
   329 		    dist=closeness;
       
   330 		}
       
   331 	    }
       
   332 	}
       
   333 	c[allocated].r=R(mostdist);
       
   334 	c[allocated].g=G(mostdist);
       
   335 	c[allocated].b=B(mostdist);
       
   336 	done[mostdist]=1;
       
   337 	fprintf(stderr,"Done %d of %d (%06x dist %d)\n",allocated+1,MAPSIZE,
       
   338 	    qRgb(c[allocated].r, c[allocated].g, c[allocated].b), dist);
       
   339     }
       
   340 
       
   341     for (int i=0; i<256; i++) {
       
   342 	printf("0x%06x,%c", qRgb(c[i].r, c[i].g, c[i].b), i%4==3 ? '\n' : ' ');
       
   343     }
       
   344 }