]>
cvs.zerfleddert.de Git - rigol/blob - png.c
10 /* Table of CRCs of all 8-bit messages. */
11 static unsigned long crc_table
[256];
13 /* Flag: has the table been computed? Initially false. */
14 static int crc_table_computed
= 0;
16 /* Make the table for a fast CRC. */
17 static void make_crc_table(void)
22 for (n
= 0; n
< 256; n
++) {
23 c
= (unsigned long) n
;
24 for (k
= 0; k
< 8; k
++) {
26 c
= 0xedb88320L
^ (c
>> 1);
32 crc_table_computed
= 1;
35 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
36 should be initialized to all 1's, and the transmitted value
37 is the 1's complement of the final running CRC (see the
38 crc() routine below). */
40 static unsigned long update_crc(unsigned long crc
, unsigned char *buf
,
43 unsigned long c
= crc
;
46 if (!crc_table_computed
)
48 for (n
= 0; n
< len
; n
++) {
49 c
= crc_table
[(c
^ buf
[n
]) & 0xff] ^ (c
>> 8);
54 /* Return the CRC of the bytes buf[0..len-1]. */
55 static unsigned long crc(unsigned char *buf
, int len
)
57 return update_crc(0xffffffffL
, buf
, len
) ^ 0xffffffffL
;
60 unsigned char *lcd2png(unsigned char *lcd
, int *len
)
62 unsigned char screen_conv
[320*234*3];
63 unsigned char lut
[256][3];
65 unsigned char *outpos
;
66 static const unsigned char png
[] = {137, 80, 78, 71, 13, 10, 26, 10};
67 static unsigned char ihdr
[] = {'I', 'H', 'D', 'R',
68 0x00, 0x00, 0x01, 0x40, /* 320 - Width */
69 0x00, 0x00, 0x00, 0xea, /* 234 - Height */
71 0x02, /* RGB Truecolor, Colour type */
72 0x00, /* Deflate, Compression method */
73 0x00, /* None, Filter method */
74 0x00 /* No interlace, Interlace method */
76 static unsigned char idat
[] = {'I', 'D', 'A', 'T'};
77 static unsigned char iend
[] = {'I', 'E', 'N', 'D'};
83 for(i
= 0; i
< 256; i
++) {
84 lut
[i
][0] = ((i
>> 6) * 0x55);
85 lut
[i
][1] = ((((i
>> 3) & 7) * 0x49) >> 1);
86 lut
[i
][2] = (((i
& 7) * 0x49) >> 1);
89 for(i
= 0; i
< sizeof(screen_conv
); i
+= 3) {
90 screen_conv
[i
] = lut
[lcd
[i
/3]][0];
91 screen_conv
[i
+1] = lut
[lcd
[i
/3]][1];
92 screen_conv
[i
+2] = lut
[lcd
[i
/3]][2];
98 if (deflateInit(&strm
, 9) != Z_OK
) {
99 perror("deflateInit");
104 image
= malloc(320*234*2); /* TODO: FIXME! */
106 memcpy(outpos
, png
, sizeof(png
));
107 outpos
+= sizeof(png
);
109 l
= htonl(sizeof(ihdr
) - 4); /* "IHDR" is not counted */
110 memcpy(outpos
, &l
, sizeof(l
));
112 memcpy(outpos
, ihdr
, sizeof(ihdr
));
113 outpos
+= sizeof(ihdr
);
114 l
= crc(ihdr
, sizeof(ihdr
));
115 memcpy(outpos
, &l
, sizeof(l
));
118 l
= htonl(sizeof(iend
) - 4); /* "IEND" is not counted */
119 memcpy(outpos
, &l
, sizeof(l
));
121 memcpy(outpos
, iend
, sizeof(iend
));
122 outpos
+= sizeof(iend
);
123 l
= crc(iend
, sizeof(iend
));
124 memcpy(outpos
, &l
, sizeof(l
));
127 *len
= (int)(outpos
- image
);