Codebase list pnm2ppa / debian/1.13-5 make_hash_ink.c
debian/1.13-5

Tree @debian/1.13-5 (Download .tar.gz)

make_hash_ink.c @debian/1.13-5raw · history · blame

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*****************************************************************
     make_hash_ink.h -generate hashed version of palette.h data
                              -----------------
     begin          : Tue Jan 24 2000
     copyright      : (C) 2000 by the pmn2ppa project
     author         : Klamer Schutte
     email          : Schutte@fel.tno.nl
******************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "palette.h"
#include "hash_ink.h"

#define SHIFTED         (1 << (shift))
#define NOTSHIFTED      (1 << (8 - shift))
#define ENTRIES_ARR     (NOTSHIFTED * NOTSHIFTED * NOTSHIFTED)

int MaxPass;

struct entry
{
  int xyz;
  union {
    int entry_nr;
    struct hash_ink *hi;
  } u;
  struct entry *next; /* this list is sorted on xyz; lowest xyz first */
};

struct entry_list
{
  struct entry *entries;
  int num_entries; 
  struct entry_list *next; /* this list is sorted in num_entries; lowest
			      numbers first */
} *start_list = 0;

inline int 
calc_xyz( struct hash_ink *hi )
{ 
  /* if these assertions fail the result is not unique */
  assert(hi->x < (1 << 3));
  assert(hi->y < (1 << 3));

  return hi->x + (hi->y << 3) + (hi->z << 6);
}

int 
add_entry2( struct entry *e, int n, struct entry **ptr_to )
{
  struct entry_list *cel, **prev_ptr, *new_el;
  struct entry *ce1, *ce2;
  static int this_entry = 0;
  int next_entry;

  assert(n > 0);

  prev_ptr = &start_list;
  for(cel = start_list; cel != 0; prev_ptr = &(cel->next), cel = cel->next)
    {
       if (cel->num_entries < n)
	continue;

      if (cel->num_entries > n)
	break;

      for(ce1 = cel->entries, ce2 = e; ce1 != 0; 
	                         ce1 = ce1->next, ce2 = ce2->next)
	{
	  assert(ce2 != 0);
	  
	  if (ce1->xyz != ce2->xyz)
	    goto next_cel;
	}
      /* OK, we have found the correct entry. Delete set *e, and
	 return the entry number */
      for(ce1 = e; ce1 != 0; ce1 = ce2)
	{
	  ce2 = ce1->next;
	  free(ce1);
	}
      
      if (ptr_to != 0)
	*ptr_to = cel->entries;
      return cel->entries->u.entry_nr;

    next_cel: ;
    }

  /* Hmmm. The set e is not found. Add the first item to the set,
     and recursively attempt it for its tail... */

  new_el = (struct entry_list *) malloc(sizeof(struct entry_list));
  /* printf(" New el %x assigned to %x\n", new_el, prev_ptr); */
  *prev_ptr = new_el;
  new_el->entries = e;
  new_el->num_entries = n;
  /* printf("new el next = %x\n", cel); */
  new_el->next = cel;

  if (e->next != 0)
    next_entry = add_entry2( e->next, n-1, &(e->next));
  else
    next_entry = -1;
  
  printf("\t/* %d */\t{ { %d, %d, %d}, %d, %d, %d, ", this_entry, 
	 e->u.hi->data[0], e->u.hi->data[1], e->u.hi->data[2], 
	 e->u.hi->x, e->u.hi->y, e->u.hi->z );
  if (next_entry == -1)
    printf("0 },\n");
  else
    printf("&entries%d[%d] },\n", MaxPass, next_entry);

  e->u.entry_nr = this_entry++;
  if (ptr_to != 0)
    *ptr_to = e;
  return e->u.entry_nr;
}

int
add_entry( struct hash_ink *hi )
{
  /* First, convert the hash_ink set to a entry set
     Second, check start_list if it is contained (done in add_entry2())
        If contained: return the entry number
        Else: add to start_list */

  struct entry *first = 0, *cur, *loop, **prev_ptr;
  struct hash_ink *chi;
  int res;
  int n = 0; /* n == number of entries in this set */

  for(chi = hi; chi != 0; chi = chi->next)
    {
      n++;
      cur = (struct entry *) malloc( sizeof(struct entry) );
      cur->xyz = calc_xyz( chi );
      cur->u.hi = chi;
      prev_ptr = &first;
      for(loop = first; loop != 0; loop = loop->next)
	{
	  if (loop->xyz >= cur->xyz)
	    {
	      cur->next = loop;
	      *prev_ptr = cur;
	      break;
	    }
	  prev_ptr = &(loop->next);
	}
      if (loop == 0)
	{
	  cur->next = 0;
	  *prev_ptr = cur;
	}
    }

  res = add_entry2( first, n, 0 );

  for(chi = hi; chi != 0; )
    {
      struct hash_ink *nhi = chi->next;
      free(chi);
      chi = nhi;
    }

  return res;
}
  
int 
main(int argc, char *argv[] )
{
  /* int MaxPass; */
  int x, y, z, r, g, b;
  int ra, ga, ba, rd, gd, bd;
  float dmin = -1, dmax = -1, d1;
  struct hash_ink /* **arr, */ *first, *prev, *cur;
  int i, n;
  int shift;
  int *entries_arr;

  if (argc != 3)
    fprintf(stderr, "Usage: %s MaxPass shift >> hash_ink.c\n", argv[0]);

  MaxPass = atoi(argv[1]);
  shift = atoi(argv[2]);

  /* arr = (struct hash_ink **) malloc( sizeof(struct hash_ink*) * ENTRIES_ARR ); 
  for(i = 0; i < ENTRIES_ARR; i++)
    arr[i] = 0; */
  entries_arr = (int *) malloc(sizeof(int) * ENTRIES_ARR);
  for(i = 0; i < ENTRIES_ARR; i++)
    entries_arr[i] = -1;

  printf("/* generated by %s %s %s */\n\n", argv[0], argv[1], argv[2] );
  printf("#include \"hash_ink.h\"\n\n");
  printf("int shift%d = %d;\n\n", MaxPass, shift);
  printf("static struct hash_ink entries%d[] = {\n", MaxPass);

  i = 0;

  for(r=0; r < 256; r += SHIFTED)
    for(g=0; g < 256; g += SHIFTED)
      for(b=0; b < 256; b += SHIFTED)
	{
	  int rr = r >> shift;
	  int gg = g >> shift;
	  int bb = b >> shift;
	  int h = (rr << (16 - shift * 2)) + (gg << (8-shift)) + bb;
/* fprintf(stderr, "%d %d %d -> %d %d %d -> %d\n", r, g, b, rr, gg, bb, h );*/
	  first = (struct hash_ink *) malloc( sizeof(struct hash_ink));
	  /* arr[h]->x = i++; */
	  cur = first;
	  cur->next = 0;
	    
	  /* Now, calculate the dmin for hash entry h.
	     Fill in *first with minimal values */
	  dmin = -1;
	  for(ra = 0; ra < SHIFTED; ra+=(SHIFTED-1))
	    for(ga = 0; ga < SHIFTED; ga += (SHIFTED-1))
	      for(ba = 0; ba < SHIFTED; ba += (SHIFTED -1))
		{
		  int R = r + ra;
		  int G = g + ga;
		  int B = b + ba;
		  int new_dmin = 0;
		  
		  for (z = 0; z < MaxPass + 1; z++)
		    for (x = 0; x < MaxPass + 1; x++)
		      for (y = 0; y < MaxPass + 1; y++)
			if (!
			    (((x == 4) && (y > 2) && (z > 2)) ||
			     ((y == 4) && (x > 2) && (z > 2)) ||
			     ((z == 4) && (y > 2) && (x > 2))))
			{
			  rd =
			    (int) R - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [0];
			  gd =
			    (int) G - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [1];
			  bd =
			    (int) B - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [2];
			  
			  d1 = /* 0.5 * */ rd * rd + gd * gd + bd * bd;

			  if ((dmin < 0) || (d1 < dmin))
			    {
			      /* fprintf(stderr, "Found min %f, %d, %d, %d (%f, %d, %d %d)\n", d1, x, y, z, dmin, cur->x, cur->y, cur->z); */
			      cur->data[0] = hp_pal[4 - x][4 - y][4 - z][0];
			      cur->data[1] = hp_pal[4 - x][4 - y][4 - z][1];
			      cur->data[2] = hp_pal[4 - x][4 - y][4 - z][2];
			      cur->x = x;
			      cur->y = y;
			      cur->z = z;
			      dmin = d1;
			      new_dmin = 1;
			    }
			}
		  
		}

	  /* Now, calculate the dmax for hash entry h.
	     Fill in *first with minimal values */
	  dmax = -1;
	  for(ra = 0; ra < SHIFTED; ra+=(SHIFTED-1))
	    for(ga = 0; ga < SHIFTED; ga += (SHIFTED-1))
	      for(ba = 0; ba < SHIFTED; ba += (SHIFTED-1))
		{
		  int R = r + ra;
		  int G = g + ga;
		  int B = b + ba;
		  
		  z = cur->z;
		  y = cur->y;
		  x = cur->x;
			{
			  rd =
			    (int) R - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [0];
			  gd =
			    (int) G - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [1];
			  bd =
			    (int) B - (int) hp_pal[4 - x][4 - y][4 -
								z]
			    [2];
			  
			  d1 = /* 0.5 * */ rd * rd + gd * gd + bd * bd;

			  if (d1 > dmax)
			    {
			      /* fprintf(stderr, "Found max %f, %d, %d, %d (%f, %f)\n", d1, x, y, z, dmax, dmin); */
			      cur->data[0] = hp_pal[4 - x][4 - y][4 - z][0];
			      cur->data[1] = hp_pal[4 - x][4 - y][4 - z][1];
			      cur->data[2] = hp_pal[4 - x][4 - y][4 - z][2];
			      cur->x = x;
			      cur->y = y;
			      cur->z = z;
			      dmax = d1;			     
			    }
			}
		}
	  
	  /* Now, for every {x,y,z} for which d1 is smaller then dmax add
	     this entry to the *first (as a linked list!) */
	  prev = cur;
	  n = 0;
	  for (z = 0; z < MaxPass + 1; z++)
	    for (x = 0; x < MaxPass + 1; x++)
	      for (y = 0; y < MaxPass + 1; y++)
		{
		if (!
		    (((x == 4) && (y > 2) && (z > 2)) ||
		     ((y == 4) && (x > 2) && (z > 2)) ||
		     ((z == 4) && (y > 2) && (x > 2))))
		  {
		    for(ra = 0; ra < SHIFTED; ra+=(SHIFTED-1))
		      for(ga = 0; ga < SHIFTED; ga += (SHIFTED-1))
			for(ba = 0; ba < SHIFTED; ba += (SHIFTED-1))
			  {
			    int R = r + ra;
			    int G = g + ga;
			    int B = b + ba;

			    rd =
			      (int) R - (int) hp_pal[4 - x][4 - y][4 -
								  z]
			      [0];
			    gd =
			      (int) G - (int) hp_pal[4 - x][4 - y][4 -
								  z]
			      [1];
			    bd =
			      (int) B - (int) hp_pal[4 - x][4 - y][4 -
								  z]
			      [2];
			  
			    d1 = /* 0.5 * */ rd * rd + gd * gd + bd * bd;
			    
			    if ((d1 < dmax) && (!((x == first->x) && 
						  (y == first->y) &&
						  (z == first->z))))
			      {
				struct hash_ink *cur = (struct hash_ink *)
				  malloc( sizeof(struct hash_ink) );
				prev->next = cur;
				cur->next = 0;
				prev = cur;
				
				/* fprintf(stderr, "Added min %f, %d, %d, %d (%f, %f, %d, %d %d)\n", d1, x, y, z, dmin, dmax, first->x, first->y, first->z); */
				
				cur->data[0] = hp_pal[4 - x][4 - y][4 - z][0];
				cur->data[1] = hp_pal[4 - x][4 - y][4 - z][1];
				cur->data[2] = hp_pal[4 - x][4 - y][4 - z][2];
				cur->x = x;
				cur->y = y;
				cur->z = z;
				n++;
				goto next_xyz; /* break out of ra gb ba loops */
			      }
			  }
		  }
		  next_xyz: ;
		}
	  /* fprintf(stderr, "%d %d %d (%d): found %d %d %d (%d,%d,%d) (%f, %f) * %d\n",
		  r, g, b, h, first->x, first->y, first->z,
		  first->data[0], first->data[1], first->data[2],
		  dmin, dmax, n ); */
	  entries_arr[h] = add_entry(first);
	}

#ifdef notdef
  printf("#include \"hash_ink.h\"\n\n");
  printf("int shift%d = %d;\n\n", MaxPass, shift);
  printf("static struct hash_ink entries%d[] = {\n", MaxPass);
  
  n = 0;
  for(i = 0; i < ENTRIES_ARR; i++)
    {
      printf("\t{ {%d, %d, %d}, %d, %d, %d, ", 
	     arr[i]->data[0], arr[i]->data[1], arr[i]->data[2], 
	     arr[i]->x, arr[i]->y, arr[i]->z );
      n++;
      cur = arr[i]->next;
      while( cur != 0 )
	{
	  printf(" &entries%d[%d] },\n", MaxPass, n);
	  printf("\t\t{ { %d, %d, %d} , %d, %d, %d, ", 
		 cur->data[0], cur->data[1], cur->data[2], 
		 cur->x, cur->y, cur->z );
	  n++;
	  cur = cur->next;
	}
      printf(" 0 },\n");
    }
#endif
  printf("};\n\n");
  
  printf("struct hash_ink *arr_max%d[] = {", MaxPass);

  n = 0;
  for(i = 0; i < ENTRIES_ARR; i++)
    {
      printf("\t&entries%d[%d], \n", MaxPass, entries_arr[i]);
#ifdef notdef
      n++;
      cur = arr[i]->next;
      while( cur != 0)
	{
	  n++;
	  cur = cur->next;
	}
#endif
    }
  
  printf("};\n\n/* end of file generated by %s %s %s (%d entries) */\n\n", 
	 argv[0], argv[1], argv[2], n);

  return 0;
}