2010-11-16 11:42:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <zlib.h>
|
2011-03-15 10:47:38 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
#include "broadway.h"
|
|
|
|
|
|
|
|
/************************************************************************
|
2010-11-24 20:57:30 +00:00
|
|
|
* Base64 functions *
|
2010-11-16 11:42:53 +00:00
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
static const char base64_alphabet[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:57:30 +00:00
|
|
|
base64_uint8 (guint8 v, char *c)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
c[0] = base64_alphabet[(v >> 0) & 0x3f];
|
|
|
|
c[1] = base64_alphabet[(v >> 6) & 0x3];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:57:30 +00:00
|
|
|
base64_uint16 (guint32 v, char *c)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
c[0] = base64_alphabet[(v >> 0) & 0x3f];
|
|
|
|
c[1] = base64_alphabet[(v >> 6) & 0x3f];
|
|
|
|
c[2] = base64_alphabet[(v >> 12) & 0xf];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void
|
2010-11-24 20:57:30 +00:00
|
|
|
base64_uint24 (guint32 v, char *c)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
c[0] = base64_alphabet[(v >> 0) & 0x3f];
|
|
|
|
c[1] = base64_alphabet[(v >> 6) & 0x3f];
|
|
|
|
c[2] = base64_alphabet[(v >> 12) & 0x3f];
|
|
|
|
c[3] = base64_alphabet[(v >> 18) & 0x3f];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:57:30 +00:00
|
|
|
base64_uint32 (guint32 v, char *c)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
c[0] = base64_alphabet[(v >> 0) & 0x3f];
|
|
|
|
c[1] = base64_alphabet[(v >> 6) & 0x3f];
|
|
|
|
c[2] = base64_alphabet[(v >> 12) & 0x3f];
|
|
|
|
c[3] = base64_alphabet[(v >> 18) & 0x3f];
|
|
|
|
c[4] = base64_alphabet[(v >> 24) & 0x3f];
|
|
|
|
c[5] = base64_alphabet[(v >> 30) & 0x2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* conversion of raw image data to uncompressed png data: uris *
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/* Table of CRCs of all 8-bit messages. */
|
|
|
|
static unsigned long crc_table[256];
|
|
|
|
|
|
|
|
/* Flag: has the table been computed? Initially false. */
|
|
|
|
static int crc_table_computed = 0;
|
|
|
|
|
|
|
|
/* Make the table for a fast CRC. */
|
|
|
|
static void
|
|
|
|
make_crc_table(void)
|
|
|
|
{
|
|
|
|
unsigned long c;
|
|
|
|
int n, k;
|
|
|
|
|
|
|
|
for (n = 0; n < 256; n++) {
|
|
|
|
c = (unsigned long) n;
|
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
if (c & 1)
|
|
|
|
c = 0xedb88320L ^ (c >> 1);
|
|
|
|
else
|
|
|
|
c = c >> 1;
|
|
|
|
}
|
|
|
|
crc_table[n] = c;
|
|
|
|
}
|
|
|
|
crc_table_computed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
update_crc(unsigned long crc, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
unsigned long c = crc;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!crc_table_computed)
|
|
|
|
make_crc_table();
|
|
|
|
for (n = 0; n < len; n++) {
|
|
|
|
c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
crc(unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BASE 65521 /* largest prime smaller than 65536 */
|
|
|
|
static unsigned long
|
|
|
|
update_adler32(unsigned long adler, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
unsigned long s1 = adler & 0xffff;
|
|
|
|
unsigned long s2 = (adler >> 16) & 0xffff;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < len; n++) {
|
|
|
|
s1 = (s1 + buf[n]) % BASE;
|
|
|
|
s2 = (s2 + s1) % BASE;
|
|
|
|
}
|
|
|
|
return (s2 << 16) + s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2010-11-24 20:57:30 +00:00
|
|
|
to_png_rgb (int w, int h, int byte_stride, guint32 *data)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar header[] = {137, 80, 78, 71, 13, 10, 26, 10};
|
|
|
|
guchar ihdr[13+12] = {0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
2010-11-16 11:42:53 +00:00
|
|
|
/* w: */ 0, 0, 0, 0, /* h: */ 0,0,0,0,
|
|
|
|
/* bpp: */ 8, /* color type: */ 2,
|
|
|
|
0, 0, 0};
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar idat_start[8] = { /* len: */0, 0, 0, 0, 'I', 'D', 'A', 'T' };
|
|
|
|
guchar iend[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82};
|
|
|
|
gsize data_size, row_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
char row_header[6];
|
2010-11-24 20:57:30 +00:00
|
|
|
guint8 *png, *p, *p_row, *p_idat;
|
|
|
|
guint32 *row;
|
2010-11-16 11:42:53 +00:00
|
|
|
unsigned long adler;
|
2010-11-24 20:57:30 +00:00
|
|
|
guint32 pixel;
|
|
|
|
gsize png_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
int x, y;
|
|
|
|
char *url, *url_base64;
|
|
|
|
int state = 0, outlen;
|
|
|
|
int save = 0;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&ihdr[8] = GUINT32_TO_BE(w);
|
|
|
|
*(guint32 *)&ihdr[12] = GUINT32_TO_BE(h);
|
|
|
|
*(guint32 *)&ihdr[21] = GUINT32_TO_BE(crc(&ihdr[4], 13 + 4));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
row_size = 1 + w * 3;
|
|
|
|
row_header[0] = 0;
|
|
|
|
row_header[1] = row_size & 0xff;
|
|
|
|
row_header[2] = (row_size >> 8) & 0xff;
|
|
|
|
row_header[3] = ~row_header[1];
|
|
|
|
row_header[4] = ~row_header[2];
|
|
|
|
row_header[5] = 0;
|
|
|
|
|
|
|
|
data_size = 2 + (6 + w * 3) * h + 4;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&idat_start[0] = GUINT32_TO_BE(data_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
png_size = sizeof(header) + sizeof(ihdr) + 12 + data_size + sizeof(iend);
|
2010-11-24 20:57:30 +00:00
|
|
|
png = g_malloc (png_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
p = png;
|
|
|
|
memcpy (p, header, sizeof(header));
|
|
|
|
p += sizeof(header);
|
|
|
|
memcpy (p, ihdr, sizeof(ihdr));
|
|
|
|
p += sizeof(ihdr);
|
|
|
|
memcpy (p, idat_start, sizeof(idat_start));
|
|
|
|
p += sizeof(idat_start);
|
|
|
|
|
|
|
|
/* IDAT data:
|
|
|
|
|
|
|
|
zlib header: 0x78, 0x01 ,
|
|
|
|
h * scanline: row_header[] + width * r,g,b
|
|
|
|
checksum: adler32
|
|
|
|
*/
|
|
|
|
|
|
|
|
p_idat = p - 4;
|
|
|
|
|
|
|
|
/* zlib header */
|
|
|
|
*p++ = 0x78;
|
|
|
|
*p++ = 0x01;
|
|
|
|
|
|
|
|
adler = 1;
|
|
|
|
|
|
|
|
/* scanline data */
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
if (y == h - 1)
|
|
|
|
row_header[0] = 1; /* final block */
|
|
|
|
memcpy (p, row_header, sizeof(row_header));
|
|
|
|
p += sizeof(row_header);
|
|
|
|
p_row = p - 1;
|
|
|
|
row = data;
|
|
|
|
data += byte_stride / 4;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
pixel = *row++;
|
|
|
|
*p++ = (pixel >> 16) & 0xff; /* red */
|
|
|
|
*p++ = (pixel >> 8) & 0xff; /* green */
|
|
|
|
*p++ = (pixel >> 0) & 0xff; /* blue */
|
|
|
|
}
|
|
|
|
adler = update_adler32(adler, p_row, p - p_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adler32 */
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(adler);
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(crc(p_idat, p - p_idat));
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
|
|
|
|
|
|
|
memcpy (p, iend, sizeof(iend));
|
|
|
|
p += sizeof(iend);
|
|
|
|
|
|
|
|
assert(p - png == png_size);
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
url = g_malloc (strlen("data:image/png;base64,") +
|
2010-11-16 11:42:53 +00:00
|
|
|
((png_size / 3 + 1) * 4 + 4) + 1);
|
|
|
|
strcpy (url, "data:image/png;base64,");
|
|
|
|
|
|
|
|
url_base64 = url + strlen("data:image/png;base64,");
|
2010-11-24 20:57:30 +00:00
|
|
|
outlen = g_base64_encode_step (png, png_size, FALSE, url_base64, &state, &save);
|
|
|
|
outlen += g_base64_encode_close (FALSE, url_base64 + outlen, &state, &save);
|
2010-11-16 11:42:53 +00:00
|
|
|
url_base64[outlen] = 0;
|
|
|
|
|
|
|
|
free (png);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2010-11-24 20:57:30 +00:00
|
|
|
to_png_rgba (int w, int h, int byte_stride, guint32 *data)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar header[] = {137, 80, 78, 71, 13, 10, 26, 10};
|
|
|
|
guchar ihdr[13+12] = {0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
2010-11-16 11:42:53 +00:00
|
|
|
/* w: */ 0, 0, 0, 0, /* h: */ 0,0,0,0,
|
|
|
|
/* bpp: */ 8, /* color type: */ 6,
|
|
|
|
0, 0, 0};
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar idat_start[8] = { /* len: */0, 0, 0, 0, 'I', 'D', 'A', 'T' };
|
|
|
|
guchar iend[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82};
|
|
|
|
gsize data_size, row_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
char row_header[6];
|
2010-11-24 20:57:30 +00:00
|
|
|
guint8 *png, *p, *p_row, *p_idat;
|
|
|
|
guint32 *row;
|
2010-11-16 11:42:53 +00:00
|
|
|
unsigned long adler;
|
2010-11-24 20:57:30 +00:00
|
|
|
guint32 pixel;
|
|
|
|
gsize png_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
int x, y;
|
|
|
|
char *url, *url_base64;
|
|
|
|
int state = 0, outlen;
|
|
|
|
int save = 0;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&ihdr[8] = GUINT32_TO_BE(w);
|
|
|
|
*(guint32 *)&ihdr[12] = GUINT32_TO_BE(h);
|
|
|
|
*(guint32 *)&ihdr[21] = GUINT32_TO_BE(crc(&ihdr[4], 13 + 4));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
row_size = 1 + w * 4;
|
|
|
|
row_header[0] = 0;
|
|
|
|
row_header[1] = row_size & 0xff;
|
|
|
|
row_header[2] = (row_size >> 8) & 0xff;
|
|
|
|
row_header[3] = ~row_header[1];
|
|
|
|
row_header[4] = ~row_header[2];
|
|
|
|
row_header[5] = 0;
|
|
|
|
|
|
|
|
data_size = 2 + (6 + w * 4) * h + 4;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&idat_start[0] = GUINT32_TO_BE(data_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
png_size = sizeof(header) + sizeof(ihdr) + 12 + data_size + sizeof(iend);
|
2010-11-24 20:57:30 +00:00
|
|
|
png = g_malloc (png_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
p = png;
|
|
|
|
memcpy (p, header, sizeof(header));
|
|
|
|
p += sizeof(header);
|
|
|
|
memcpy (p, ihdr, sizeof(ihdr));
|
|
|
|
p += sizeof(ihdr);
|
|
|
|
memcpy (p, idat_start, sizeof(idat_start));
|
|
|
|
p += sizeof(idat_start);
|
|
|
|
|
|
|
|
/* IDAT data:
|
|
|
|
|
|
|
|
zlib header: 0x78, 0x01 ,
|
|
|
|
h * scanline: row_header[] + width * r,g,b,a
|
|
|
|
checksum: adler32
|
|
|
|
*/
|
|
|
|
|
|
|
|
p_idat = p - 4;
|
|
|
|
|
|
|
|
/* zlib header */
|
|
|
|
*p++ = 0x78;
|
|
|
|
*p++ = 0x01;
|
|
|
|
|
|
|
|
adler = 1;
|
|
|
|
|
|
|
|
/* scanline data */
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
if (y == h - 1)
|
|
|
|
row_header[0] = 1; /* final block */
|
|
|
|
memcpy (p, row_header, sizeof(row_header));
|
|
|
|
p += sizeof(row_header);
|
|
|
|
p_row = p - 1;
|
|
|
|
row = data;
|
|
|
|
data += byte_stride / 4;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
pixel = *row++;
|
|
|
|
*p++ = (pixel >> 16) & 0xff; /* red */
|
|
|
|
*p++ = (pixel >> 8) & 0xff; /* green */
|
|
|
|
*p++ = (pixel >> 0) & 0xff; /* blue */
|
|
|
|
*p++ = (pixel >> 24) & 0xff; /* alpha */
|
|
|
|
}
|
|
|
|
adler = update_adler32(adler, p_row, p - p_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adler32 */
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(adler);
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(crc(p_idat, p - p_idat));
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
|
|
|
|
|
|
|
memcpy (p, iend, sizeof(iend));
|
|
|
|
p += sizeof(iend);
|
|
|
|
|
|
|
|
assert(p - png == png_size);
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
url = g_malloc (strlen("data:image/png;base64,") +
|
2010-11-16 11:42:53 +00:00
|
|
|
((png_size / 3 + 1) * 4 + 4) + 1);
|
|
|
|
strcpy (url, "data:image/png;base64,");
|
|
|
|
|
|
|
|
url_base64 = url + strlen("data:image/png;base64,");
|
2010-11-24 20:57:30 +00:00
|
|
|
outlen = g_base64_encode_step (png, png_size, FALSE, url_base64, &state, &save);
|
|
|
|
outlen += g_base64_encode_close (FALSE, url_base64 + outlen, &state, &save);
|
2010-11-16 11:42:53 +00:00
|
|
|
url_base64[outlen] = 0;
|
|
|
|
|
|
|
|
free (png);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static char *
|
2010-11-24 20:57:30 +00:00
|
|
|
to_png_a (int w, int h, int byte_stride, guint8 *data)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar header[] = {137, 80, 78, 71, 13, 10, 26, 10};
|
|
|
|
guchar ihdr[13+12] = {0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
2010-11-16 11:42:53 +00:00
|
|
|
/* w: */ 0, 0, 0, 0, /* h: */ 0,0,0,0,
|
|
|
|
/* bpp: */ 8, /* color type: */ 4,
|
|
|
|
0, 0, 0};
|
2010-11-24 20:57:30 +00:00
|
|
|
guchar idat_start[8] = { /* len: */0, 0, 0, 0, 'I', 'D', 'A', 'T' };
|
|
|
|
guchar iend[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82};
|
|
|
|
gsize data_size, row_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
char row_header[6];
|
2010-11-24 20:57:30 +00:00
|
|
|
guint8 *png, *p, *p_row, *p_idat;
|
|
|
|
guint8 *row;
|
2010-11-16 11:42:53 +00:00
|
|
|
unsigned long adler;
|
2010-11-24 20:57:30 +00:00
|
|
|
guint32 pixel;
|
|
|
|
gsize png_size;
|
2010-11-16 11:42:53 +00:00
|
|
|
int x, y;
|
|
|
|
char *url, *url_base64;
|
|
|
|
int state = 0, outlen;
|
|
|
|
int save = 0;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&ihdr[8] = GUINT32_TO_BE(w);
|
|
|
|
*(guint32 *)&ihdr[12] = GUINT32_TO_BE(h);
|
|
|
|
*(guint32 *)&ihdr[21] = GUINT32_TO_BE(crc(&ihdr[4], 13 + 4));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
row_size = 1 + w * 2;
|
|
|
|
row_header[0] = 0;
|
|
|
|
row_header[1] = row_size & 0xff;
|
|
|
|
row_header[2] = (row_size >> 8) & 0xff;
|
|
|
|
row_header[3] = ~row_header[1];
|
|
|
|
row_header[4] = ~row_header[2];
|
|
|
|
row_header[5] = 0;
|
|
|
|
|
|
|
|
data_size = 2 + (6 + w * 2) * h + 4;
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)&idat_start[0] = GUINT32_TO_BE(data_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
png_size = sizeof(header) + sizeof(ihdr) + 12 + data_size + sizeof(iend);
|
2010-11-24 20:57:30 +00:00
|
|
|
png = g_malloc (png_size);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
p = png;
|
|
|
|
memcpy (p, header, sizeof(header));
|
|
|
|
p += sizeof(header);
|
|
|
|
memcpy (p, ihdr, sizeof(ihdr));
|
|
|
|
p += sizeof(ihdr);
|
|
|
|
memcpy (p, idat_start, sizeof(idat_start));
|
|
|
|
p += sizeof(idat_start);
|
|
|
|
|
|
|
|
/* IDAT data:
|
|
|
|
|
|
|
|
zlib header: 0x78, 0x01 ,
|
|
|
|
h * scanline: row_header[] + width * r,g,b,a
|
|
|
|
checksum: adler32
|
|
|
|
*/
|
|
|
|
|
|
|
|
p_idat = p - 4;
|
|
|
|
|
|
|
|
/* zlib header */
|
|
|
|
*p++ = 0x78;
|
|
|
|
*p++ = 0x01;
|
|
|
|
|
|
|
|
adler = 1;
|
|
|
|
|
|
|
|
/* scanline data */
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
if (y == h - 1)
|
|
|
|
row_header[0] = 1; /* final block */
|
|
|
|
memcpy (p, row_header, sizeof(row_header));
|
|
|
|
p += sizeof(row_header);
|
|
|
|
p_row = p - 1;
|
|
|
|
row = data;
|
|
|
|
data += byte_stride / 4;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
pixel = *row++;
|
|
|
|
*p++ = 0x00; /* gray */
|
|
|
|
*p++ = pixel; /* alpha */
|
|
|
|
}
|
|
|
|
adler = update_adler32(adler, p_row, p - p_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adler32 */
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(adler);
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
2010-11-24 20:57:30 +00:00
|
|
|
*(guint32 *)p = GUINT32_TO_BE(crc(p_idat, p - p_idat));
|
2010-11-16 11:42:53 +00:00
|
|
|
p += 4;
|
|
|
|
|
|
|
|
memcpy (p, iend, sizeof(iend));
|
|
|
|
p += sizeof(iend);
|
|
|
|
|
|
|
|
assert(p - png == png_size);
|
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
url = g_malloc (strlen("data:image/png;base64,") +
|
2010-11-16 11:42:53 +00:00
|
|
|
((png_size / 3 + 1) * 4 + 4) + 1);
|
|
|
|
strcpy (url, "data:image/png;base64,");
|
|
|
|
|
|
|
|
url_base64 = url + strlen("data:image/png;base64,");
|
2010-11-24 20:57:30 +00:00
|
|
|
outlen = g_base64_encode_step (png, png_size, FALSE, url_base64, &state, &save);
|
|
|
|
outlen += g_base64_encode_close (FALSE, url_base64 + outlen, &state, &save);
|
2010-11-16 11:42:53 +00:00
|
|
|
url_base64[outlen] = 0;
|
|
|
|
|
|
|
|
free (png);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Basic I/O primitives *
|
|
|
|
************************************************************************/
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
struct BroadwayOutput {
|
2010-11-16 11:42:53 +00:00
|
|
|
int fd;
|
|
|
|
gzFile *zfd;
|
2010-11-24 21:12:29 +00:00
|
|
|
int error;
|
2011-03-10 13:58:47 +00:00
|
|
|
guint32 serial;
|
2010-11-24 21:12:29 +00:00
|
|
|
};
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
static void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write_raw (BroadwayOutput *output,
|
2010-11-24 20:57:30 +00:00
|
|
|
const void *buf, gsize count)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
gssize res;
|
2010-11-16 11:42:53 +00:00
|
|
|
int errsave;
|
|
|
|
const char *ptr = (const char *)buf;
|
|
|
|
|
2010-11-24 21:12:29 +00:00
|
|
|
if (output->error)
|
|
|
|
return;
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
while (count > 0)
|
|
|
|
{
|
2010-11-24 20:40:25 +00:00
|
|
|
res = write(output->fd, ptr, count);
|
2010-11-16 11:42:53 +00:00
|
|
|
if (res == -1)
|
|
|
|
{
|
|
|
|
errsave = errno;
|
|
|
|
if (errsave == EINTR)
|
|
|
|
continue;
|
2010-11-24 21:12:29 +00:00
|
|
|
output->error = TRUE;
|
|
|
|
return;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2010-11-24 21:12:29 +00:00
|
|
|
output->error = TRUE;
|
|
|
|
return;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
count -= res;
|
|
|
|
ptr += res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (BroadwayOutput *output,
|
2010-11-24 20:57:30 +00:00
|
|
|
const void *buf, gsize count)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
gssize res;
|
2010-11-16 11:42:53 +00:00
|
|
|
const char *ptr = (const char *)buf;
|
|
|
|
|
2010-11-24 21:12:29 +00:00
|
|
|
if (output->error)
|
|
|
|
return;
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
while (count > 0)
|
|
|
|
{
|
2010-11-24 20:40:25 +00:00
|
|
|
res = gzwrite(output->zfd, ptr, count);
|
2010-11-16 11:42:53 +00:00
|
|
|
if (res == -1)
|
|
|
|
{
|
2010-11-24 21:12:29 +00:00
|
|
|
output->error = TRUE;
|
|
|
|
return;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2010-11-24 21:12:29 +00:00
|
|
|
output->error = TRUE;
|
|
|
|
return;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
count -= res;
|
|
|
|
ptr += res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write_header (BroadwayOutput *output)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
char *header;
|
|
|
|
|
|
|
|
header =
|
2010-11-18 21:21:16 +00:00
|
|
|
"HTTP/1.1 200 OK\r\n"
|
2010-11-16 11:42:53 +00:00
|
|
|
"Content-type: multipart/x-mixed-replace;boundary=x\r\n"
|
|
|
|
"Content-Encoding: gzip\r\n"
|
|
|
|
"\r\n";
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write_raw (output,
|
2010-11-16 11:42:53 +00:00
|
|
|
header, strlen (header));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-24 20:40:25 +00:00
|
|
|
send_boundary (BroadwayOutput *output)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
char *boundary =
|
|
|
|
"--x\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (output, boundary, strlen (boundary));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
BroadwayOutput *
|
2011-03-10 13:58:47 +00:00
|
|
|
broadway_output_new(int fd, guint32 serial)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:40:25 +00:00
|
|
|
BroadwayOutput *output;
|
2011-03-15 10:47:38 +00:00
|
|
|
int flag = 1;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
output = g_new0 (BroadwayOutput, 1);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
output->fd = fd;
|
2011-03-10 13:58:47 +00:00
|
|
|
output->serial = serial;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-15 10:47:38 +00:00
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write_header (output);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
output->zfd = gzdopen(fd, "wb");
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
/* Need an initial multipart boundary */
|
2010-11-24 20:40:25 +00:00
|
|
|
send_boundary (output);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
return output;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 21:12:29 +00:00
|
|
|
broadway_output_free (BroadwayOutput *output)
|
|
|
|
{
|
|
|
|
if (output->zfd)
|
|
|
|
gzclose (output->zfd);
|
|
|
|
else
|
|
|
|
close (output->fd);
|
|
|
|
free (output);
|
|
|
|
}
|
|
|
|
|
2011-03-10 13:58:47 +00:00
|
|
|
guint32
|
|
|
|
broadway_output_get_next_serial (BroadwayOutput *output)
|
|
|
|
{
|
|
|
|
return output->serial;
|
|
|
|
}
|
|
|
|
|
2010-11-24 21:12:29 +00:00
|
|
|
int
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_flush (BroadwayOutput *output)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:40:25 +00:00
|
|
|
send_boundary (output);
|
|
|
|
gzflush (output->zfd, Z_SYNC_FLUSH);
|
2010-11-24 21:12:29 +00:00
|
|
|
return !output->error;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Core rendering operations *
|
|
|
|
************************************************************************/
|
|
|
|
|
2011-03-10 13:58:47 +00:00
|
|
|
#define HEADER_LEN (1+6)
|
2011-03-10 12:59:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
append_uint16 (guint32 v, char *buf, int *p)
|
|
|
|
{
|
|
|
|
base64_uint16 (v, &buf[*p]);
|
|
|
|
*p += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
append_uint32 (guint32 v, char *buf, int *p)
|
|
|
|
{
|
|
|
|
base64_uint32 (v, &buf[*p]);
|
|
|
|
*p += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_header(BroadwayOutput *output, char *buf, char op)
|
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = 0;
|
|
|
|
buf[p++] = op;
|
2011-03-10 13:58:47 +00:00
|
|
|
append_uint32 (output->serial++, buf, &p);
|
2011-03-10 12:59:30 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_copy_rectangles (BroadwayOutput *output, int id,
|
2010-11-16 11:42:53 +00:00
|
|
|
BroadwayRect *rects, int n_rects,
|
|
|
|
int dx, int dy)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int len, i, p;
|
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
len = HEADER_LEN + 3 + 3 + 3*4*n_rects + 3 + 3;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
buf = g_malloc (len);
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'b');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (n_rects, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
for (i = 0; i < n_rects; i++)
|
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint16 (rects[i].x, buf, &p);
|
|
|
|
append_uint16 (rects[i].y, buf, &p);
|
|
|
|
append_uint16 (rects[i].width, buf, &p);
|
|
|
|
append_uint16 (rects[i].height, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint16 (dx, buf, &p);
|
|
|
|
append_uint16 (dy, buf, &p);
|
|
|
|
|
|
|
|
assert (p == len);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (output, buf, len);
|
2010-11-16 11:42:53 +00:00
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
|
2011-04-01 14:29:03 +00:00
|
|
|
void
|
2011-03-14 10:46:43 +00:00
|
|
|
broadway_output_grab_pointer (BroadwayOutput *output,
|
|
|
|
int id,
|
2011-04-01 14:29:03 +00:00
|
|
|
gboolean owner_event)
|
2011-03-14 10:46:43 +00:00
|
|
|
{
|
2011-04-01 14:29:03 +00:00
|
|
|
char buf[HEADER_LEN + 3 + 1];
|
2011-03-14 10:46:43 +00:00
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'g');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
buf[p++] = owner_event ? '1': '0';
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
guint32
|
2011-04-01 14:29:03 +00:00
|
|
|
broadway_output_ungrab_pointer (BroadwayOutput *output)
|
2011-03-14 10:46:43 +00:00
|
|
|
{
|
2011-04-01 14:29:03 +00:00
|
|
|
char buf[HEADER_LEN];
|
2011-03-14 10:46:43 +00:00
|
|
|
guint32 serial;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
serial = output->serial;
|
|
|
|
p = write_header (output, buf, 'u');
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
|
|
|
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2011-04-05 10:27:43 +00:00
|
|
|
broadway_output_new_surface(BroadwayOutput *output,
|
|
|
|
int id, int x, int y, int w, int h,
|
|
|
|
gboolean is_temp)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-04-05 10:27:43 +00:00
|
|
|
char buf[HEADER_LEN + 16];
|
2011-03-10 12:59:30 +00:00
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 's');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (x, buf, &p);
|
|
|
|
append_uint16 (y, buf, &p);
|
|
|
|
append_uint16 (w, buf, &p);
|
|
|
|
append_uint16 (h, buf, &p);
|
2011-04-05 10:27:43 +00:00
|
|
|
buf[p++] = is_temp ? '1' : '0';
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
assert (p == sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_show_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 3];
|
|
|
|
int p;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'S');
|
|
|
|
append_uint16 (id, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_hide_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 3];
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'H');
|
|
|
|
append_uint16 (id, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
assert (p == sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_destroy_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 3];
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'd');
|
|
|
|
append_uint16 (id, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
assert (p == sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_move_surface(BroadwayOutput *output, int id, int x, int y)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 9];
|
|
|
|
int p;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'm');
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (x, buf, &p);
|
|
|
|
append_uint16 (y, buf, &p);
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 11:52:10 +00:00
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_resize_surface(BroadwayOutput *output, int id, int w, int h)
|
2010-11-23 11:52:10 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 9];
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'r');
|
|
|
|
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (w, buf, &p);
|
|
|
|
append_uint16 (h, buf, &p);
|
2010-11-23 11:52:10 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
assert (p == sizeof (buf));
|
2010-11-23 11:52:10 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-23 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 10:02:46 +00:00
|
|
|
void
|
|
|
|
broadway_output_set_transient_for (BroadwayOutput *output,
|
|
|
|
int id,
|
|
|
|
int parent_id)
|
|
|
|
{
|
|
|
|
char buf[HEADER_LEN + 6];
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'p');
|
|
|
|
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (parent_id, buf, &p);
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_put_rgb (BroadwayOutput *output, int id, int x, int y,
|
2010-11-16 11:42:53 +00:00
|
|
|
int w, int h, int byte_stride, void *data)
|
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 15];
|
2010-11-24 20:57:30 +00:00
|
|
|
gsize len;
|
2010-11-16 11:42:53 +00:00
|
|
|
char *url;
|
2011-03-10 12:59:30 +00:00
|
|
|
int p;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'i');
|
|
|
|
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (x, buf, &p);
|
|
|
|
append_uint16 (y, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
url = to_png_rgb (w, h, byte_stride, (guint32*)data);
|
2010-11-16 11:42:53 +00:00
|
|
|
len = strlen (url);
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint32 (len, buf, &p);
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (output, url, len);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
free (url);
|
|
|
|
}
|
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
typedef struct {
|
|
|
|
int x1, y1;
|
|
|
|
int x2, y2;
|
|
|
|
} BroadwayBox;
|
|
|
|
|
|
|
|
static int
|
2010-11-22 12:07:49 +00:00
|
|
|
is_any_x_set (unsigned char *data,
|
|
|
|
int box_x1, int box_x2,
|
|
|
|
int x1, int x2, int y, int *x_set,
|
|
|
|
int byte_stride)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-22 12:07:49 +00:00
|
|
|
int w ;
|
2010-11-24 20:57:30 +00:00
|
|
|
guint32 *ptr;
|
2010-11-22 12:07:49 +00:00
|
|
|
|
|
|
|
if (x1 < box_x1)
|
|
|
|
x1 = box_x1;
|
|
|
|
|
|
|
|
if (x2 > box_x2)
|
|
|
|
x2 = box_x2;
|
|
|
|
|
|
|
|
w = x2 - x1;
|
|
|
|
if (w > 0)
|
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
ptr = (guint32 *)(data + y * byte_stride + x1 * 4);
|
2010-11-22 12:07:49 +00:00
|
|
|
while (w-- > 0)
|
|
|
|
{
|
|
|
|
if (*ptr != 0)
|
|
|
|
{
|
|
|
|
if (x_set)
|
|
|
|
*x_set = x1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
x1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2010-11-21 23:00:37 +00:00
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-22 12:07:49 +00:00
|
|
|
|
|
|
|
#define EXTEND_X_FUZZ 10
|
|
|
|
#define EXTEND_Y_FUZZ 10
|
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
static int
|
|
|
|
extend_x_range (unsigned char *data,
|
|
|
|
int box_x1, int box_y1,
|
|
|
|
int box_x2, int box_y2,
|
|
|
|
int *x1, int *x2, int y,
|
|
|
|
int byte_stride)
|
|
|
|
{
|
|
|
|
int extended = 0;
|
2010-11-22 12:07:49 +00:00
|
|
|
int new_x;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-22 12:07:49 +00:00
|
|
|
while (is_any_x_set (data, box_x1, box_x2, *x1 - EXTEND_X_FUZZ, *x1, y, &new_x, byte_stride))
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-22 12:07:49 +00:00
|
|
|
*x1 = new_x;
|
|
|
|
extended = 1;
|
2010-11-21 23:00:37 +00:00
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-22 12:07:49 +00:00
|
|
|
while (is_any_x_set (data, box_x1, box_x2, *x2, *x2 + EXTEND_X_FUZZ, y, &new_x, byte_stride))
|
2010-11-21 23:00:37 +00:00
|
|
|
{
|
2010-11-22 12:07:49 +00:00
|
|
|
*x2 = new_x + 1;
|
|
|
|
extended = 1;
|
2010-11-21 23:00:37 +00:00
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
return extended;
|
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
static int
|
|
|
|
extend_y_range (unsigned char *data,
|
|
|
|
int box_x1, int box_y1,
|
|
|
|
int box_x2, int box_y2,
|
|
|
|
int x1, int x2, int *y,
|
|
|
|
int byte_stride)
|
|
|
|
{
|
|
|
|
int extended = 0;
|
|
|
|
int found_set;
|
2010-11-22 12:07:49 +00:00
|
|
|
int yy, y2;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
while (*y < box_y2)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-21 23:00:37 +00:00
|
|
|
found_set = 0;
|
2010-11-22 12:07:49 +00:00
|
|
|
|
|
|
|
y2 = *y + EXTEND_Y_FUZZ;
|
|
|
|
if (y2 > box_y2)
|
|
|
|
y2 = box_y2;
|
|
|
|
|
|
|
|
for (yy = y2; yy > *y + 1; yy--)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-22 12:07:49 +00:00
|
|
|
if (is_any_x_set (data, box_x1, box_x2, x1, x2, yy - 1, NULL, byte_stride))
|
2010-11-21 23:00:37 +00:00
|
|
|
{
|
|
|
|
found_set = 1;
|
|
|
|
break;
|
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
2010-11-21 23:00:37 +00:00
|
|
|
if (!found_set)
|
2010-11-16 11:42:53 +00:00
|
|
|
break;
|
2010-11-22 12:07:49 +00:00
|
|
|
*y = yy;
|
2010-11-21 23:00:37 +00:00
|
|
|
extended = 1;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
return extended;
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
static void
|
|
|
|
rgba_find_rects_extents (unsigned char *data,
|
|
|
|
int box_x1, int box_y1,
|
|
|
|
int box_x2, int box_y2,
|
|
|
|
int x, int y,
|
|
|
|
BroadwayBox *rect,
|
|
|
|
int byte_stride)
|
|
|
|
{
|
|
|
|
int x1, x2, y1, y2, yy;
|
|
|
|
int extended;
|
|
|
|
|
|
|
|
x1 = x;
|
|
|
|
x2 = x + 1;
|
|
|
|
y1 = y;
|
|
|
|
y2 = y + 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Expand maximally for all known rows */
|
|
|
|
do
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-21 23:00:37 +00:00
|
|
|
extended = 0;
|
|
|
|
|
|
|
|
for (yy = y1; yy < y2; yy++)
|
|
|
|
extended |= extend_x_range (data,
|
|
|
|
box_x1, box_y1,
|
|
|
|
box_x2, box_y2,
|
|
|
|
&x1, &x2, yy,
|
|
|
|
byte_stride);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
2010-11-21 23:00:37 +00:00
|
|
|
while (extended);
|
|
|
|
}
|
|
|
|
while (extend_y_range(data,
|
|
|
|
box_x1, box_y1,
|
|
|
|
box_x2, box_y2,
|
|
|
|
x1, x2, &y2,
|
|
|
|
byte_stride));
|
|
|
|
|
|
|
|
rect->x1 = x1;
|
|
|
|
rect->x2 = x2;
|
|
|
|
rect->y1 = y1;
|
|
|
|
rect->y2 = y2;
|
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
static void
|
|
|
|
rgba_find_rects_sub (unsigned char *data,
|
|
|
|
int box_x1, int box_y1,
|
|
|
|
int box_x2, int box_y2,
|
|
|
|
int byte_stride,
|
|
|
|
BroadwayBox **rects,
|
|
|
|
int *n_rects, int *alloc_rects)
|
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
guint32 *line;
|
2010-11-21 23:00:37 +00:00
|
|
|
BroadwayBox rect;
|
|
|
|
int x, y;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
if (box_x1 == box_x2 || box_y1 == box_y2)
|
|
|
|
return;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
for (y = box_y1; y < box_y2; y++)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
line = (guint32 *)(data + y * byte_stride + box_x1 * 4);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
for (x = box_x1; x < box_x2; x++)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-21 23:00:37 +00:00
|
|
|
if (*line != 0)
|
|
|
|
{
|
|
|
|
rgba_find_rects_extents (data,
|
|
|
|
box_x1, box_y1, box_x2, box_y2,
|
|
|
|
x, y, &rect, byte_stride);
|
|
|
|
if (*n_rects == *alloc_rects)
|
|
|
|
{
|
|
|
|
(*alloc_rects) *= 2;
|
2010-11-24 20:57:30 +00:00
|
|
|
*rects = g_renew (BroadwayBox, *rects, *alloc_rects);
|
2010-11-21 23:00:37 +00:00
|
|
|
}
|
|
|
|
(*rects)[*n_rects] = rect;
|
|
|
|
(*n_rects)++;
|
|
|
|
rgba_find_rects_sub (data,
|
|
|
|
box_x1, rect.y1,
|
|
|
|
rect.x1, rect.y2,
|
|
|
|
byte_stride,
|
|
|
|
rects, n_rects, alloc_rects);
|
|
|
|
rgba_find_rects_sub (data,
|
|
|
|
rect.x2, rect.y1,
|
|
|
|
box_x2, rect.y2,
|
|
|
|
byte_stride,
|
|
|
|
rects, n_rects, alloc_rects);
|
|
|
|
rgba_find_rects_sub (data,
|
|
|
|
box_x1, rect.y2,
|
|
|
|
box_x2, box_y2,
|
|
|
|
byte_stride,
|
|
|
|
rects, n_rects, alloc_rects);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line++;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-21 23:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BroadwayBox *
|
|
|
|
rgba_find_rects (unsigned char *data,
|
|
|
|
int w, int h, int byte_stride,
|
|
|
|
int *n_rects)
|
|
|
|
{
|
|
|
|
BroadwayBox *rects;
|
|
|
|
int alloc_rects;
|
|
|
|
|
|
|
|
alloc_rects = 20;
|
2010-11-24 20:57:30 +00:00
|
|
|
rects = g_new (BroadwayBox, alloc_rects);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
*n_rects = 0;
|
|
|
|
rgba_find_rects_sub (data,
|
|
|
|
0, 0, w, h, byte_stride,
|
|
|
|
&rects, n_rects, &alloc_rects);
|
|
|
|
|
|
|
|
return rects;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_put_rgba (BroadwayOutput *output, int id, int x, int y,
|
2010-11-21 18:52:05 +00:00
|
|
|
int w, int h, int byte_stride, void *data)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 15];
|
2010-11-24 20:57:30 +00:00
|
|
|
gsize len;
|
2010-11-16 11:42:53 +00:00
|
|
|
char *url;
|
2010-11-21 23:00:37 +00:00
|
|
|
BroadwayBox *rects;
|
2011-03-10 12:59:30 +00:00
|
|
|
int p, i, n_rects;
|
2010-11-24 20:57:30 +00:00
|
|
|
guint8 *subdata;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
rects = rgba_find_rects (data, w, h, byte_stride, &n_rects);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
for (i = 0; i < n_rects; i++)
|
|
|
|
{
|
2010-11-24 20:57:30 +00:00
|
|
|
subdata = (guint8 *)data + rects[i].x1 * 4 + rects[i].y1 * byte_stride;
|
2010-11-19 20:54:17 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'i');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (x + rects[i].x1, buf, &p);
|
|
|
|
append_uint16 (y + rects[i].y1, buf, &p);
|
2010-11-19 20:54:17 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
url = to_png_rgba (rects[i].x2 - rects[i].x1,
|
|
|
|
rects[i].y2 - rects[i].y1,
|
2010-11-24 20:57:30 +00:00
|
|
|
byte_stride, (guint32*)subdata);
|
2010-11-21 23:00:37 +00:00
|
|
|
len = strlen (url);
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint32 (len, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 13:58:47 +00:00
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (output, url, len);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
free (url);
|
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-21 23:00:37 +00:00
|
|
|
free (rects);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 08:50:07 +00:00
|
|
|
void
|
|
|
|
broadway_output_surface_flush (BroadwayOutput *output,
|
|
|
|
int id)
|
|
|
|
{
|
|
|
|
char buf[HEADER_LEN + 3];
|
|
|
|
int p;
|
|
|
|
|
|
|
|
p = write_header (output, buf, 'f');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
|
|
|
|
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
#if 0
|
|
|
|
static void
|
2010-11-24 20:40:25 +00:00
|
|
|
send_image_a (BroadwayOutput *output, int id, int x, int y,
|
2010-11-24 20:57:30 +00:00
|
|
|
int w, int h, int byte_stride, guint8 *data)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2011-03-10 12:59:30 +00:00
|
|
|
char buf[HEADER_LEN + 15];
|
2010-11-24 20:57:30 +00:00
|
|
|
gsize len;
|
2010-11-16 11:42:53 +00:00
|
|
|
char *url;
|
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
p = write_header (output, buf, 'i');
|
|
|
|
append_uint16 (id, buf, &p);
|
|
|
|
append_uint16 (x, buf, &p);
|
|
|
|
append_uint16 (y, buf, &p);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
url = to_png_a (w, h, byte_stride, data);
|
|
|
|
len = strlen (url);
|
2011-03-10 12:59:30 +00:00
|
|
|
append_uint32 (len, buf, &p);
|
|
|
|
|
|
|
|
assert (p == sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-03-10 12:59:30 +00:00
|
|
|
broadway_output_write (output, buf, sizeof (buf));
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_write (output, url, len);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
free (url);
|
|
|
|
}
|
|
|
|
#endif
|