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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/. */
/* */
/* NXCOMPEXT, NX protocol compression and NX extensions to this software */
/* are copyright of NoMachine. Redistribution and use of the present */
/* software is allowed according to terms specified in the file LICENSE */
/* which comes in the source distribution. */
/* */
/* Check http://www.nomachine.com/licensing.html for applicability. */
/* */
/* NX and NoMachine are trademarks of NoMachine S.r.l. */
/* */
/* All rigths reserved. */
/* */
/**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Xutil.h"
#include "NXlib.h"
#include "Mask.h"
#include "Pgn.h"
#define PANIC
#define WARNING
#undef TEST
#undef DEBUG
/*
* Selected ZLIB compression level.
*/
#define PNG_Z_LEVEL 4
/*
* Local function prototypes.
*/
static void PrepareRowForPng(CARD8 *dst, int y, int count);
static void PrepareRowForPng24(CARD8 *dst, int y, int count);
static void PrepareRowForPng16(CARD8 *dst, int y, int count);
static void PrepareRowForPng32(CARD8 *dst, int y, int count);
static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length);
static void PngFlushData(png_structp png_ptr);
/*
* Image characteristics.
*/
static int bytesPerLine;
static int byteOrder;
static CARD8 bitsPerPixel;
static CARD16 redMax, greenMax, blueMax;
static CARD8 redShift, greenShift, blueShift;
/*
* Other variables used for the Png
* encoding.
*/
png_byte color_type;
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
static char *pngCompBuf;
static int pngDataLen;
static char *pngBeforeBuf = NULL;
/*
* Allocate data for the compressed image.
* We need to ensure that there is enough
* space to include the palette and the
* header.
*/
#define PNG_DEST_SIZE(width, height) ((width) * 3 * (height) + 1024 + 256)
/*
* Just for debug purposes.
*/
#ifdef DEBUG
static int pngId;
static char pngName[10];
static FILE *pngFile;
#endif
int PngCompareColorTable(NXColorTable *c1, NXColorTable *c2)
{
return (c1 -> pixel - c2 -> pixel);
}
#define NB_COLOR_MAX 256
int NXCreatePalette32(XImage *src_image, NXColorTable *color_table, CARD8 *image_index, int nb_max)
{
int x, y, t, p;
CARD8 *fbptr;
CARD32 pixel;
fbptr = (CARD8 *) (src_image -> data);
/*
* TODO: Find a more intelligent way to
* estimate the number of colors.
*/
memset(color_table, 0, nb_max * sizeof(NXColorTable));
for (x = 0, p = 0; x < src_image -> height; x++)
{
for (y = 0; y < src_image -> width; y++)
{
if (byteOrder == LSBFirst)
{
pixel = (CARD32) *(fbptr + 3);
pixel = (pixel << 8) | (CARD32) *(fbptr + 2);
pixel = (pixel << 8) | (CARD32) *(fbptr + 1);
pixel = (pixel << 8) | (CARD32) *fbptr;
}
else
{
pixel = (CARD32) *fbptr;
pixel = (pixel << 8) | (CARD32) *(fbptr + 1);
pixel = (pixel << 8) | (CARD32) *(fbptr + 2);
pixel = (pixel << 8) | (CARD32) *(fbptr + 3);
}
fbptr += 4;
for (t = 0; t < nb_max; t++)
{
if (color_table[t].found == 0)
{
color_table[t].pixel = pixel;
color_table[t].found = 1;
p++;
image_index[((x * src_image -> width) + y)] = t;
break;
}
else if ((CARD32)(color_table[t].pixel) == pixel)
{
image_index[((x * src_image -> width) + y)] = t;
break;
}
}
if (p == nb_max)
{
return nb_max + 1;
}
}
}
return p;
}
int NXCreatePalette16(XImage *src_image, NXColorTable *color_table, CARD8 *image_index, int nb_max)
{
int x, y, t, p;
CARD8 *fbptr;
CARD16 pixel;
fbptr = (CARD8 *) (src_image -> data);
/*
* TODO: Find a more intelligent way to
* estimate the number of colors.
*/
memset(color_table, 0, nb_max * sizeof(NXColorTable));
for (x = 0, p = 0; x < src_image -> height; x++)
{
for (y = 0; y < src_image -> width; y++)
{
if (byteOrder == LSBFirst)
{
pixel = (CARD16) *(fbptr + 1);
pixel = (pixel << 8) | (CARD16) *fbptr;
}
else
{
pixel = (CARD16) *fbptr;
pixel = (pixel << 8) | (CARD16) *(fbptr + 1);
}
fbptr += 2;
for (t = 0; t < nb_max; t++)
{
if (color_table[t].found == 0)
{
color_table[t].pixel = pixel;
color_table[t].found = 1;
p++;
image_index[((x * src_image -> width) + y)] = t;
break;
}
else if ((color_table[t].pixel) == pixel)
{
image_index[((x * src_image -> width) + y)] = t;
break;
}
}
/*
* In case the number of 16bit words is not even
* we have 2 padding bytes that we have to skip.
*/
if ((y == src_image -> width - 1) && (src_image -> width % 2 == 1)) fbptr += 2;
if (p == nb_max)
{
return nb_max + 1;
}
}
}
return p;
}
char *PngCompressData(XImage *image, int *compressed_size)
{
unsigned int num = 0;
CARD8 *srcBuf;
int dy, w, h;
int nb_colors;
NXColorTable color_table[NB_COLOR_MAX];
CARD8 *image_index;
image_index = (CARD8 *) malloc((image -> height) * (image -> width) * sizeof(CARD8));
/*
* TODO: Be sure the padded bytes are cleaned.
* It would be better to set to zero the bytes
* that are not alligned to the word boundary
* at the end of the procedure.
*/
memset(image_index, 0, (image -> height) * (image -> width) * sizeof(CARD8));
*compressed_size = 0;
pngDataLen = 0;
/*
* Initialize the image stuff.
*/
bitsPerPixel = image -> bits_per_pixel;
bytesPerLine = image -> bytes_per_line;
byteOrder = image -> byte_order;
if (bitsPerPixel < 15)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Can't compress images with [%d] bits per pixel.\n",
bitsPerPixel);
#endif
return NULL;
}
redShift = FindLSB(image -> red_mask) - 1;
greenShift = FindLSB(image -> green_mask) - 1;
blueShift = FindLSB(image -> blue_mask) - 1;
redMax = image -> red_mask >> redShift;
greenMax = image -> green_mask >> greenShift;
blueMax = image -> blue_mask >> blueShift;
w = image -> width;
h = image -> height;
pngBeforeBuf = image -> data;
#ifdef DEBUG
fprintf(stderr, "******PngCompressData: Compressing image with width [%d] height [%d].\n",
w, h );
#endif
/*
* Initialize the PNG stuff.
*/
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Failed creating the png_create_write_struct.\n");
#endif
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Failed creating the png_create_info_struct.\n");
#endif
png_destroy_write_struct(&png_ptr, NULL);
return NULL;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Error during compression initialization.\n");
#endif
png_destroy_write_struct(&png_ptr, &info_ptr);
return NULL;
}
/*
* Be sure we allocate enough data.
*/
#ifdef TEST
fprintf(stderr, "******PngCompressData: Allocating [%d] bytes for the destination data.\n",
PNG_DEST_SIZE(w, h));
#endif
pngCompBuf = malloc(PNG_DEST_SIZE(w, h));
if (pngCompBuf == NULL)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Error allocating [%d] bytes for the Png data.\n",
PNG_DEST_SIZE(w, h));
#endif
return NULL;
}
png_set_write_fn(png_ptr, (void *) pngCompBuf, PngWriteData, PngFlushData);
if (setjmp(png_jmpbuf(png_ptr)))
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Error writing the header.\n");
#endif
png_destroy_write_struct(&png_ptr, &info_ptr);
free(pngCompBuf);
return NULL;
}
png_set_compression_level(png_ptr, PNG_Z_LEVEL);
if (bitsPerPixel == 16)
{
nb_colors = NXCreatePalette16(image, color_table, image_index, NB_COLOR_MAX);
}
else
{
nb_colors = NXCreatePalette32(image, color_table, image_index, NB_COLOR_MAX);
}
if (nb_colors <= NB_COLOR_MAX)
{
color_type = PNG_COLOR_TYPE_PALETTE;
}
else
{
color_type = PNG_COLOR_TYPE_RGB;
}
png_set_IHDR(png_ptr, info_ptr, w, h,
8, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
palette = png_malloc(png_ptr, sizeof(*palette) * 256);
/*
* TODO: Do we need to clean these bytes?
*
* memset(palette, 0, sizeof(*palette) * 256);
*/
for (num = 0; num < 256 && color_table[num].found != 0; num++)
{
if (bitsPerPixel == 24)
{
palette[num].red = (color_table[num].pixel >> redShift) & redMax;
palette[num].green = (color_table[num].pixel >> greenShift) & greenMax;
palette[num].blue = color_table[num].pixel >> blueShift & blueMax;
}
else
{
int inRed, inGreen, inBlue;
inRed = (color_table[num].pixel >> redShift) & redMax;
inGreen = (color_table[num].pixel >> greenShift) & greenMax;
inBlue = color_table[num].pixel >> blueShift & blueMax;
palette[num].red = (CARD8)((inRed * 255 + redMax / 2) / redMax);
palette[num].green = (CARD8)((inGreen * 255 + greenMax / 2) / greenMax);
palette[num].blue = (CARD8)((inBlue * 255 + blueMax / 2) / blueMax);
}
#ifdef DEBUG
fprintf(stderr, "******PngCompressData: pixel[%d] r[%d] g[%d] b[%d].\n",
(int) color_table[num].pixel,palette[num].red,palette[num].green,palette[num].blue);
#endif
}
png_set_PLTE(png_ptr, info_ptr, palette, num);
#ifdef DEBUG
fprintf(stderr, "******PngCompressedData: Setting palette.\n");
#endif
}
/*
* End of palette.
*/
png_write_info(png_ptr, info_ptr);
/*
* Allocate space for one line of
* the image, 3 bytes per pixel.
*/
#ifdef DEBUG
fprintf(stderr, "******PngCompressedData: Initialization finished.\n");
#endif
if (setjmp(png_jmpbuf(png_ptr)))
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Error while writing the image rows.\n");
#endif
png_destroy_write_struct(&png_ptr, &info_ptr);
free(pngCompBuf);
return NULL;
}
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
srcBuf = (CARD8 *) malloc(w * sizeof(CARD8));
if (srcBuf == NULL)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Cannot allocate [%d] bytes.\n",
(int) (w * sizeof(CARD8)));
#endif
return NULL;
}
/*
* TODO: Be sure the padded bytes are cleaned.
* It would be better to set to zero the bytes
* that are not alligned to the word boundary
* at the end of the procedure.
*/
memset(srcBuf, 0, w * sizeof(CARD8));
}
else
{
srcBuf = (CARD8 *) malloc(w * 3 * sizeof(CARD8));
/*
* TODO: See above.
*/
memset(srcBuf, 0, w * 3 * sizeof(CARD8));
}
if (srcBuf == NULL)
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! Cannot allocate [%d] bytes.\n",
w * 3);
#endif
free(pngCompBuf);
return NULL;
}
for (dy = 0; dy < h; dy++)
{
if (color_type == PNG_COLOR_TYPE_RGB)
{
PrepareRowForPng(srcBuf, dy, w);
}
else
{
memcpy(srcBuf, image_index + (dy * w), w);
}
png_write_row(png_ptr, srcBuf);
}
#ifdef DEBUG
fprintf(stderr, "******PngCompressedData: Compression finished. Lines handled [%d,%d].\n",
dy, h);
#endif
free(srcBuf);
free(image_index);
if (setjmp(png_jmpbuf(png_ptr)))
{
#ifdef PANIC
fprintf(stderr, "******PngCompressData: PANIC! error during end of write.\n");
#endif
png_destroy_write_struct(&png_ptr, &info_ptr);
free(pngCompBuf);
return NULL;
}
png_write_end(png_ptr, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_free(png_ptr, palette);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
/*
* Check the size of the resulting data.
*/
if (pngDataLen > 0)
{
#ifdef DEBUG
int i = 0;
fprintf(stderr, "******PngCompressedData: Compressed size [%d].\n",
pngDataLen);
pngId++;
sprintf(pngName, "png%d", pngId);
pngFile = fopen(pngName, "w");
for (i = 0; i < pngDataLen; i++)
{
fprintf(pngFile, "%c", *(pngCompBuf + i));
}
fclose(pngFile);
#endif
*compressed_size = pngDataLen;
return pngCompBuf;
}
else
{
#ifdef DEBUG
fprintf(stderr, "******PngCompressedData: PANIC! Invalid size of the compressed data [%d].\n",
pngDataLen);
#endif
free(pngCompBuf);
return NULL;
}
}
static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length)
{
memcpy(((char *) png_get_io_ptr(png_ptr) + pngDataLen), data, length);
pngDataLen += length;
}
static void PngFlushData(png_structp png_ptr)
{
}
void PrepareRowForPng(CARD8 *dst, int y, int count)
{
if (bitsPerPixel == 32)
{
if (redMax == 0xff &&
greenMax == 0xff &&
blueMax == 0xff)
{
PrepareRowForPng24(dst, y, count);
}
else
{
PrepareRowForPng32(dst, y, count);
}
}
else if (bitsPerPixel == 24)
{
memcpy(dst, pngBeforeBuf + y * bytesPerLine, count * 3);
}
else
{
/*
* 16 bpp assumed.
*/
PrepareRowForPng16(dst, y, count);
}
}
void PrepareRowForPng24(CARD8 *dst, int y, int count)
{
CARD8 *fbptr;
CARD32 pix;
fbptr = (CARD8 *) (pngBeforeBuf + y * bytesPerLine);
while (count--)
{
if (byteOrder == LSBFirst)
{
pix = (CARD32) *(fbptr + 2);
pix = (pix << 8) | (CARD32) *(fbptr+1);
pix = (pix << 8) | (CARD32) *fbptr;
}
else
{
pix = (CARD32) *(fbptr + 1);
pix = (pix << 8) | (CARD32) *(fbptr + 2);
pix = (pix << 8) | (CARD32) *(fbptr + 3);
}
*dst++ = (CARD8)(pix >> redShift);
*dst++ = (CARD8)(pix >> greenShift);
*dst++ = (CARD8)(pix >> blueShift);
fbptr+=4;
}
}
#define DEFINE_PNG_GET_ROW_FUNCTION(bpp) \
\
void PrepareRowForPng##bpp(CARD8 *dst, int y, int count) \
{ \
CARD8 *fbptr; \
CARD##bpp pix; \
int inRed, inGreen, inBlue; \
int i; \
\
fbptr = (CARD8 *) (pngBeforeBuf + y * bytesPerLine); \
\
while (count--) \
{ \
pix = 0; \
\
if (byteOrder == LSBFirst) \
{ \
for (i = (bpp >> 3) - 1; i >= 0; i--) \
{ \
pix = (pix << 8) | (CARD32) *(fbptr + i); \
} \
} \
else \
{ \
for (i = 0; i < (bpp >> 3); i++) \
{ \
pix = (pix << 8) | (CARD32) *(fbptr + i); \
} \
} \
\
fbptr += (bpp >> 3); \
\
inRed = (int) \
(pix >> redShift & redMax); \
inGreen = (int) \
(pix >> greenShift & greenMax); \
inBlue = (int) \
(pix >> blueShift & blueMax); \
*dst++ = (CARD8)((inRed * 255 + redMax / 2) / \
redMax); \
*dst++ = (CARD8)((inGreen * 255 + greenMax / 2) / \
greenMax); \
*dst++ = (CARD8)((inBlue * 255 + blueMax / 2) / \
blueMax); \
} \
}
DEFINE_PNG_GET_ROW_FUNCTION(16)
DEFINE_PNG_GET_ROW_FUNCTION(32)
|