File: encodeham8.c

package info (click to toggle)
ppmtoagafb 0.8
  • links: PTS
  • area: main
  • in suites: slink
  • size: 84 kB
  • ctags: 78
  • sloc: ansic: 908; python: 122; makefile: 55
file content (155 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download
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
/* HAM8 encoding, based on the jpegAGA HAM8 encoding
 * Originally by Gnther Rhrich
 * Converted to C by Chris Lawrence <quango@themall.net>
 * 
 *  () Copyright 1996-98 by Chris Lawrence
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License.  See the file COPYING in the main directory of the Linux
 *  distribution for more details.
 *
 * To do: translate this into human-readable C
 *        (as opposed to assembleresque C).
 *        gcc might then be able to optimize it decently.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <ppm.h>

extern u_char ColorTable[64 * 3];
extern u_char *ColorCache;
extern u_int16_t Mult_Table[256 * 2];

static u_int16_t compute_error(u_char * orig, u_char * chosen);
void EncodeHAM8(pixel * pixrow, pixval maxval, u_char * yham, u_int16_t xsize,
		int do_histogram);

static u_int16_t compute_error(u_char * orig, u_char * chosen)
{
  u_int16_t ret, x;

  x = abs(orig[0] - chosen[0]);
  ret = Mult_Table[x];
  x = abs(orig[1] - chosen[1]);
  ret += Mult_Table[x];
  x = abs(orig[2] - chosen[2]);
  ret += Mult_Table[x];

  return ret;
}

void EncodeHAM8(pixel * pixrow, pixval maxval, u_char * yham, u_int16_t xsize,
		int do_histogram)
{
  u_char orig_cols[3], left[3], cache, *finham;
  u_char best_color = 3, colcount = 0, ham_offset, tmp, tmp2, change_val;
  u_int32_t CacheOffset, offset;
  u_int16_t err, min_error;
  pixel p;

  memcpy(left, ColorTable, sizeof(left));

  for( finham = (yham + xsize - 1); yham <= finham; )
  {
    PPM_DEPTH(p, (*pixrow), maxval, 63);
    pixrow++;

    orig_cols[0] = PPM_GETB(p);
    orig_cols[1] = PPM_GETR(p);
    orig_cols[2] = PPM_GETG(p);

    if( left[0] == orig_cols[0] && left[1] == orig_cols[1] &&
        left[2] == orig_cols[2] )
    {
      ham_offset = colcount;
      colcount = (colcount + 1) % 3;

      left[ham_offset] = orig_cols[ham_offset];
      *(yham++) = (orig_cols[ham_offset] << 2) | (ham_offset + 1);
    }
    else
    {
      min_error = 65535;

      offset = (orig_cols[1] << 12) | (orig_cols[2] << 6) | orig_cols[0];
      cache = ColorCache[offset];
      if (!cache)
      {
	CacheOffset = offset;
	best_color = offset = 0;

	do
	{
	  err = compute_error(orig_cols, &ColorTable[offset]);
	  offset += 3;

	  if (err < min_error)
	  {
	    min_error = err;
	    best_color = offset;
	  }
	}
	while (err > 0 && offset < 192);

	if (err)
	{
	  ColorCache[CacheOffset] = best_color;
	}
	else
	{
	  min_error = compute_error(orig_cols, &ColorTable[offset - 3]);
	}
      }
      else
      {
	if (!do_histogram)
	{
	  *(yham++) = ((cache / 3) - 1) << 2;
          memcpy(left, &ColorTable[best_color - 3], sizeof(left));
	  continue;
	}

	best_color = cache;
	min_error = compute_error(orig_cols, &ColorTable[best_color - 3]);
      }

      change_val = orig_cols[0];
      tmp2 = abs(orig_cols[0] - left[0]);
      ham_offset = 0;

      tmp = abs(orig_cols[1] - left[1]);
      if (tmp > tmp2)
      {
	change_val = orig_cols[1];
	ham_offset = 1;
	tmp2 = tmp;
      }

      tmp = abs(orig_cols[2] - left[2]);
      if (tmp > tmp2)
      {
	change_val = orig_cols[2];
	ham_offset = 2;
      }

      tmp = left[ham_offset];
      left[ham_offset] = change_val;
      err = compute_error(orig_cols, left);

      if (min_error >= err)
      {
        /* Use HAM */
        *(yham++) = (change_val << 2) | (ham_offset + 1);
      }
      else
      {
        *(yham++) = ((best_color / 3) - 1) << 2;
        memcpy(left, &ColorTable[best_color - 3], sizeof(left));
      }
    }
  }

  return;
}