File: palettes.c

package info (click to toggle)
squeak-vm 1%3A4.10.2.2614-4.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,284 kB
  • ctags: 15,344
  • sloc: ansic: 75,096; cs: 11,191; objc: 5,494; sh: 3,170; asm: 1,533; cpp: 449; pascal: 372; makefile: 366; awk: 103
file content (246 lines) | stat: -rw-r--r-- 7,081 bytes parent folder | download | duplicates (8)
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
#include "ccvt.h"

#include <stdio.h>
#include "palettes.h"


struct palette_info {
   int       id;
   char*     name;
   float     bytesPerPixel;
   Converter converterFunction24;
   Converter converterFunction16;
   int       depth;
};


inline void BGR24ToRGB32(int width, int height, const void *voidSrc, void *voidDst) {
   int i;

   unsigned char* dest = (unsigned char*) voidDst;
   unsigned char* src  = (unsigned char*) voidSrc;

   unsigned char red, green, blue;

   const unsigned int widthTimesHeight = width * height;
   for (i = 0; i < widthTimesHeight; i++) {
      blue  = *src++;
      green = *src++;
      red   = *src++;

      *dest++ = red;
      *dest++ = green;
      *dest++ = blue;
      *dest++ = 255;
   }
}

inline void RGB565ToRGB24(int width, int height, const void *voidSrc, void *voidDst) {
   int i;

   const unsigned char* src  = (unsigned char*) voidSrc;
         unsigned char* dest = (unsigned char*) voidDst;

   unsigned char b1, b2;

   unsigned char red, green, blue;

   const unsigned int widthTimesHeight = width * height;
   for (i = 0; i < widthTimesHeight; i++) {
      b2 = *src++;
      b1 = *src++;

      red   = b1 & 248 /* 11111000 */;
      green = (b1 << 5) | (b2 >> 5);
      blue  = b2 << 3;

      *dest++ = red;
      *dest++ = green;
      *dest++ = blue;
   }
}

inline void BGR24ToRGB24(int width, int height, const void *voidSrc, void *voidDst) {
   int i;

   unsigned char* dest = (unsigned char*) voidDst;
   unsigned char* src  = (unsigned char*) voidSrc;

   unsigned char red, green, blue;

   const unsigned int widthTimesHeight = width * height;
   for (i = 0; i < widthTimesHeight; i++) {
       blue  = *src++;
       green = *src++;
       red   = *src++;

       *dest++ = red;
       *dest++ = green;
       *dest++ = blue;
   }
}

inline unsigned char clip(const int pixel) {
    int result;

    result = ((pixel < 0) ? 0 : pixel);
    return (unsigned char) ((result < 255) ? result : 255);
}


// from: http://en.wikipedia.org/wiki/YUV422
inline void YUV444toRGB888(const unsigned char y,
			   const unsigned char u,
			   const unsigned char v,
			   unsigned char* dest) {
   const int C = y - 16;
   const int D = u - 128;
   const int E = v - 128;

   dest[0] = clip(( 298 * C           + 409 * E + 128) >> 8);
   dest[1] = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8);
   dest[2] = clip(( 298 * C + 516 * D           + 128) >> 8);
}


// from: http://en.wikipedia.org/wiki/YUV422
inline void YUYVToRGB24(int width, int height, const void *voidSrc, void *voidDst) {
   int i;

   const unsigned char* src  = (unsigned char*) voidSrc;
         unsigned char* dest = (unsigned char*) voidDst;

   unsigned char u, y1, v, y2;

   const unsigned int widthTimesHeight = width * height;
   for (i = 0; i < widthTimesHeight; i += 2) {
      y1 = *src++;
      u  = *src++;
      y2 = *src++;
      v  = *src++;

      YUV444toRGB888(y1, u, v, dest);
      dest += 3;

      YUV444toRGB888(y2, u, v, dest);
      dest += 3;
   }
}

inline void YUV422ToRGB24(int width, int height, const void *voidSrc, void *voidDst) {
   int i;

   unsigned char* dest = (unsigned char*) voidDst;
   unsigned char* src  = (unsigned char*) voidSrc;

   unsigned char u, y1, v, y2;

   const unsigned int widthTimesHeight = width * height;
   for (i = 0; i < widthTimesHeight; i += 2) {
      u  = *src++;
      y1 = *src++;
      v  = *src++;
      y2 = *src++;

      YUV444toRGB888(y1, u, v, dest);
      dest += 3;

      YUV444toRGB888(y2, u, v, dest);
      dest += 3;
   }
}

static struct palette_info palette_info_list[] = {
   { 0,  "UNKOWN - Unkown palette",                        0.0,                0,                  0,  0},
   { 1,  "GREY - Linear greyscale",                        0.0,                0,                  0,  8},
   { 2,  "HI240 - High 240 cube (BT848)",                  0.0,                0,                  0,  8},
   { 3,  "RGB565 - 565 16 bit RGB",                        2.0,    RGB565ToRGB24,                  0, 16},
   { 4,  "RGB24 - 24bit RGB",                              3.0,     BGR24ToRGB24,                  0, 24},
   { 5,  "RGB32 - 32bit RGB",                              4.0, ccvt_bgr32_rgb24,                  0, 32},
   { 6,  "RGB555 - 555 15bit RGB",                         0.0,                0,                  0, 16},
   { 7,  "YUV422 - YUV422 capture",                        1.5,    YUV422ToRGB24,                  0, 16},
   { 8,  "YUYV",                                           2.0,      YUYVToRGB24,                  0, 16},
   { 9,  "UYVY - The great thing about standards is ...",  0.0,                0,                  0, 16},
   {10,  "YUV420",                                         1.5,  ccvt_420p_rgb24,                  0, 16},
   {11,  "YUV411 - YUV411 capture",                        0.0,                0,                  0, 12},
   {12,  "RAW - RAW capture (BT848)",                      0.0,                0,                  0,  8},
   {13,  "YUV422P - YUV 4:2:2 Planar",                     0.0,                0,                  0, 16},
   {14,  "YUV411P - YUV 4:1:1 Planar",                     0.0,                0,                  0, 12},
   {15,  "YUV420P - YUV 4:2:0 Planar",                     1.5,  ccvt_420p_rgb24,                  0, 12},
   {16,  "YUV410P - YUV 4:1:0 Planar",                     0.0,                0,                  0,  9}
};


inline float paletteBytesPerPixel(int palette) {
   float bytesPerPixel;
   if (palette < 1 || palette > 16) {
      fprintf(stdout, "* Invalid palette=%d\n", palette);
      return 0;
   }

   bytesPerPixel = palette_info_list[palette].bytesPerPixel;
   if (bytesPerPixel == 0) {
      fprintf(stdout, "* Palette=%d not yet supported\n", palette);
      return 0;
   }

   return bytesPerPixel;
}


inline char* paletteName(int palette) {
   if (palette == 0) {
      return "*automatic*";
   }
   if (palette < 0 || palette > 16) {
      return "*UNKOWN*";
   }
   
   return palette_info_list[palette].name;
}

inline Converter converterFunction(const int palette) {
   Converter funct;

   if (palette < 1 || palette > 16) {
      fprintf(stdout, "* Invalid palette=%d\n", palette);
      return 0;
   }

   funct = palette_info_list[palette].converterFunction24;
   if (funct == 0) {
      fprintf(stdout, "* Palette=%d not yet supported\n", palette);
      return 0;
   }

   return funct;
}

inline int paletteConvert24(const int palette,
                            const int width, const int height,
                            const void *src, void *dst) {
   Converter funct = converterFunction(palette);
   if (funct == 0) {
      return 0;
   }

   funct(width, height, src, dst);
   return 1;
}


inline int paletteDepth(int palette) {
   int depth;
   if (palette < 1 || palette > 16) {
      fprintf(stdout, "* Invalid palette=%d\n", palette);
      return 0;
   }

   depth = palette_info_list[palette].depth;
   if (depth == 0) {
      fprintf(stdout, "* Palette=%d not yet supported\n", palette);
      return 0;
   }

   return depth;
}