-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.c
374 lines (306 loc) · 8.7 KB
/
mirror.c
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
// first OpenVG program
// Anthony Starks ([email protected])
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "EGL/egl.h"
#include "GLES/gl.h"
#include "bcm_host.h"
//#include "fontinfo.h"
//#include "shapes.h"
#include <jpeglib.h>
#include <gd.h>
#define null ((void *)0L)
#define RGB(r, g, b) ((r << 16) + (g << 8) + b)
#define R(c) ((c & 0xFF0000) >> 16)
#define G(c) ((c & 0xFF00) >> 8)
#define B(c) (c & 0xFF)
#define GREY(c) ((R(c) + G(c) + B(c)) / 3)
#define MIX(a, b, c) (a + (((b - a) * c) / 0xFF))
#define RGBMIX(c1, c2, d) \
(RGB(MIX(R(c1), R(c2), d), MIX(G(c1), G(c2), d), MIX(B(c1), G(c2), d)))
#define xxxFAKE_INPUT 1
uint32_t hsv2rgb(float hue, float sat, float val)
{
double hh, p, q, t, ff;
long i;
int red, green, blue;
hh = hue;
if(sat <= 0.0)
{
red = val;
green = val;
blue = val;
}
else
{
if(hh >= 360.0) hh = hh-360.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = val * (1.0 - sat);
q = val * (1.0 - (sat * ff));
t = val * (1.0 - (sat * (1.0 - ff)));
switch(i) {
case 0:
red = val * 255.0;
green = t * 255.0;
blue = p * 255.0;
break;
case 1:
red = q * 255.0;
green = val * 255.0;
blue = p * 255.0;
break;
case 2:
red = p * 255.0;
green = val * 255.0;
blue = t * 255.0;
break;
case 3:
red = p * 255.0;
green = q * 255.0;
blue = val * 255.0;
break;
case 4:
red = t * 255.0;
green = p * 255.0;
blue = val * 255.0;
break;
case 5:
default:
red = val * 255.0;
green = p * 255.0;
blue = q * 255.0;
break;
}
}
uint32_t result = (((uint32_t)red) << 16) |
(((uint32_t)green) << 8) |
(((uint32_t)blue));
return result;
}
VGImage loadJpeg(const char *filename, VGubyte **outData, unsigned int *outWidth, unsigned int *outHeight ) {
FILE *infile;
struct jpeg_decompress_struct jdc;
struct jpeg_error_mgr jerr;
JSAMPARRAY buffer;
unsigned int bstride;
unsigned int bbpp;
VGImage img;
VGubyte *data;
unsigned int width;
unsigned int height;
unsigned int dstride;
unsigned int dbpp;
VGubyte *brow;
VGubyte *drow;
unsigned int x;
unsigned int lilEndianTest = 1;
VGImageFormat rgbaFormat;
// Check for endianness
if (((unsigned char *)&lilEndianTest)[0] == 1)
{
fprintf(stdout, "VG_sABGR_8888\n");
rgbaFormat = VG_sABGR_8888;
}
else
{
fprintf(stdout, "VG_sRGBA_8888\n");
rgbaFormat = VG_sRGBA_8888;
}
// Try to open image file
infile = fopen(filename, "rb");
if (infile == NULL) {
printf("Failed opening '%s' for reading!\n", filename);
return VG_INVALID_HANDLE;
}
// Setup default error handling
jdc.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&jdc);
// Set input file
jpeg_stdio_src(&jdc, infile);
// Read header and start
jpeg_read_header(&jdc, TRUE);
jpeg_start_decompress(&jdc);
width = jdc.output_width;
height = jdc.output_height;
if (outWidth)
*outWidth = width;
if (outHeight)
*outHeight = height;
// Allocate buffer using jpeg allocator
bbpp = jdc.output_components;
bstride = width * bbpp;
buffer = (*jdc.mem->alloc_sarray)
((j_common_ptr) & jdc, JPOOL_IMAGE, bstride, 1);
// Allocate image data buffer
dbpp = 4;
dstride = width * dbpp;
data = (VGubyte *) malloc(dstride * height);
// Iterate until all scanlines processed
while (jdc.output_scanline < height) {
// Read scanline into buffer
jpeg_read_scanlines(&jdc, buffer, 1);
drow = data + (height - jdc.output_scanline) * dstride;
brow = buffer[0];
// Expand to RGBA
for (x = 0; x < width; ++x, drow += dbpp, brow += bbpp) {
switch (bbpp) {
case 4:
drow[0] = brow[0];
drow[1] = brow[1];
drow[2] = brow[2];
drow[3] = brow[3];
break;
case 3:
drow[0] = brow[0];
drow[1] = brow[1];
drow[2] = brow[2];
drow[3] = 255;
break;
}
}
}
// Create VG image
img = vgCreateImage(rgbaFormat, width, height, VG_IMAGE_QUALITY_BETTER);
vgImageSubData(img, data, dstride, rgbaFormat, 0, 0, width, height);
// Cleanup
jpeg_destroy_decompress(&jdc);
fclose(infile);
if (outData)
{
*outData = data;
}
else
free(data);
return img;
}
int getLuma(x, y, w, h)
{
}
int initCalled = 0;
int windowWidth, windowHeight;
unsigned char *buf;
void myrect(int x, int y, int w, int h, int luma)
{
uint32_t color = hsv2rgb(1.0f * (rand()%360), 1.0, (float)(luma / 255.0));
unsigned char *pScanline = &buf[y*windowWidth*4 + x*4];
while (h-- > 0)
{
int xwalk;
for (xwalk=0; xwalk<w;++xwalk)
{
pScanline[xwalk*4+3] = 255;
pScanline[xwalk*4+0] = color >> 16;
pScanline[xwalk*4+1] = (color >> 8) & 255;
pScanline[xwalk*4+2] = color & 255;
}
pScanline += windowWidth*4;
}
}
typedef void (*pixelPainter)(int x, int y, int w, int h, int cx, int cy, int rx, int ry);
void PaintLumaOnStaticHues(int x, int y, int w, int h, int cx, int cy, int rx, int ry, luma)
{
myrect(cx-rx, cy-ry, rx*2, ry*2, luma);
}
pixelPainter painters[] = {
PaintLumaOnStaticHues
};
void processMirrorFrame(uint8_t *pFrame, int width, int height)
{
if (!initCalled)
{
init(&windowWidth, &windowHeight);
initCalled = 1;
buf = malloc(windowWidth * windowHeight * 4);
}
// fprintf(stdout, "Calling start x=%d, y=%d\n", w, h);
// fprintf(stdout, "VGErr=%d\n", vgGetError());
#ifdef FAKE_INPUT
// Load jpeg as source data
unsigned char *pRawFrame;
(void) loadJpeg("ill.jpg", &pRawFrame, &width, &height);
// Now iterate the frame and convert to YaYb
unsigned char *pSrc = pFrame;
unsigned char *pDst = pFrame;
int totalPixels = width * height;
while (totalPixels--)
{
unsigned int luma = (pSrc[0] + pSrc[1] + pSrc[2]) / 3;
*pDst = luma;
pSrc +- 4;
pDst += 2;
}
pFrame = pRawFrame;
#endif
// Always seed with the same value so we get the same color sequence
srand(822);
Start(windowWidth, windowHeight);
int walk;
// Clear frame to black
memset(buf, 0, windowWidth*windowHeight*4);
int x,y;
int yCells = 34;
int xCells = 24;
int cellWidth = width/xCells;
int cellHeight = height/yCells;
int windowCellWidth = windowWidth / xCells;
int windowCellHeight = windowHeight / yCells;
for(y = 0; y < yCells; y++)
for(x = 0; x < xCells; x++)
{
uint8_t *pWalker = &pFrame[y*cellHeight*width*2 + x*cellWidth*2];
uint32_t accumulator = 0;
int xincell, yincell;
for (yincell=0; yincell<cellHeight;++yincell)
{
for (xincell=0; xincell<cellWidth;++xincell)
{
accumulator += pWalker[xincell*2];
}
pWalker += width*2;
}
accumulator = accumulator / (cellWidth*cellHeight);
int centerX = x*windowCellWidth + windowCellWidth/2;
int centerY = y*windowCellHeight + windowCellHeight/2;
centerY = windowHeight-centerY;
int radius = ((VGfloat)accumulator) / 256.0 * windowCellHeight;
radius = radius / 2;
radius = windowCellHeight / 2;
myrect(buf,
centerX-radius, centerY-radius, radius*2, radius*2, accumulator & 255);
// ircle(x*windowCellWidth - windowCellWidth/2, y*windowCellHeight - windowCellHeight/2, ((VGfloat)accumulator) / 256.0 * windowCellWidth);
// int c = gdImageGetPixel(pFrame, x, y);
// pFrame->tpixels[y][x] = 0xff000000 | R(c) | (G(c)<<8) | (B(c)<<16) ;
// gdImageSetPixel(pFrame, x, y, 0xff0000ff );
}
makeimage(0.0,0.0,windowWidth, windowHeight, buf);
// makeimage(100.0,100.0, gdImageSX(pFrame)+2, gdImageSY(pFrame), (char*)pFrame->tpixels[0]);
End();
}
int notmain() {
int width, height;
char s[3];
init(&width, &height); // Graphics initialization
VGubyte *pPixels = null;
VGImage vgImage;
unsigned int imgWidth, imgHeight;
vgImage = loadJpeg("poop.jpg", &pPixels, &imgWidth, &imgHeight);
fprintf(stdout, "%xhh %xhh %xhh %xhh\n", pPixels[0], pPixels[1], pPixels[2], pPixels[3]);
long int pixelCount = imgWidth * imgHeight;
VGubyte *pFiddle = pPixels;
while (pixelCount--)
{
pFiddle[3] = 0;
pFiddle += 4;
}
vgImageSubData(vgImage, pPixels, imgWidth*4, VG_sRGBA_8888, 0, 0, imgWidth, imgHeight);
vgSetPixels(100, 100, vgImage, 0, 0, imgWidth, imgHeight);
End(); // End the picture
fgets(s, 2, stdin); // look at the pic, end with [RETURN]
finish(); // Graphics cleanup
exit(0);
}