compress images in record device
authorMatthias Kramm <kramm@quiss.org>
Wed, 31 Mar 2010 01:30:39 +0000 (18:30 -0700)
committerMatthias Kramm <kramm@quiss.org>
Wed, 31 Mar 2010 01:30:39 +0000 (18:30 -0700)
lib/devices/record.c
lib/png.c
lib/png.h

index d0d7715..f79d32c 100644 (file)
@@ -29,6 +29,7 @@
 #ifdef HAVE_IO_H
 #include <io.h>
 #endif
+#include <zlib.h>
 #include <string.h>
 #include <assert.h>
 #include "../gfxdevice.h"
 #include "../bitio.h"
 #include "../log.h"
 #include "../os.h"
+#include "../png.h"
 #include "record.h"
 
+//#define STATS
+
 typedef struct _state {
     char*last_string[16];
     gfxcolor_t last_color[16];
     gfxmatrix_t last_matrix[16];
+
+#ifdef STATS
+    int size_matrices;
+    int size_positions;
+    int size_images;
+    int size_lines;
+    int size_colors;
+    int size_fonts;
+    int size_chars;
+#endif
 } state_t;
 
 typedef struct _internal {
@@ -83,39 +97,43 @@ typedef struct _internal_result {
 #define LINE_LINETO 0x0f
 #define LINE_SPLINETO 0x10
 
-static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
-{
-    internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
-    writer_writeU8(&i->w, OP_SETPARAM);
-    writer_writeString(&i->w, key);
-    writer_writeString(&i->w, value);
-    return 1;
-}
+/* ----------------- reading/writing of low level primitives -------------- */
 
-static void dumpLine(writer_t*w, gfxline_t*line)
+static void dumpLine(writer_t*w, state_t*state, gfxline_t*line)
 {
     while(line) {
        if(line->type == gfx_moveTo) {
            writer_writeU8(w, LINE_MOVETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
+#ifdef STATS
+           state->size_lines += 1+8+8;
+#endif
        } else if(line->type == gfx_lineTo) {
            writer_writeU8(w, LINE_LINETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
+#ifdef STATS
+           state->size_lines += 1+8+8;
+#endif
        } else if(line->type == gfx_splineTo) {
            writer_writeU8(w, LINE_SPLINETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
            writer_writeDouble(w, line->sx);
            writer_writeDouble(w, line->sy);
+#ifdef STATS
+           state->size_lines += 1+8+8+8+8;
+#endif
        }
        line = line->next;
     }
     writer_writeU8(w, OP_END);
+#ifdef STATS
+    state->size_lines += 1;
+#endif
 }
-static gfxline_t* readLine(reader_t*r)
+static gfxline_t* readLine(reader_t*r, state_t*s)
 {
     gfxline_t*start = 0, *pos = 0;
     while(1) {
@@ -147,16 +165,83 @@ static gfxline_t* readLine(reader_t*r)
     }
     return start;
 }
-static gfximage_t readImage(reader_t*r)
+
+#define COMPRESS_IMAGES
+static void dumpImage(writer_t*w, state_t*state, gfximage_t*img)
+{
+    int oldpos = w->pos;
+    writer_writeU16(w, img->width);
+    writer_writeU16(w, img->height);
+#ifdef COMPRESS_IMAGES
+    //45.2% images (3055340 bytes) (without filter)
+    //39.9% images (2458904 bytes) (with filter, Z_BEST_SPEED)
+    //35.3% images (2027305 bytes) (with filter, Z_BEST_COMPRESSION)
+    unsigned char*filter = malloc(img->height);
+    int y;
+    gfxcolor_t*image = malloc(img->width*img->height*sizeof(gfxcolor_t));
+    for(y=0;y<img->height;y++) {
+       filter[y] = png_apply_filter_32(
+               (void*)&image[y*img->width], 
+               (void*)&img->data[y*img->width], img->width, y);
+    }
+    int size = img->width*img->height;
+    uLongf compressdata_size = compressBound(size*sizeof(gfxcolor_t));
+    void*compressdata = malloc(compressdata_size);
+    compress2(compressdata, &compressdata_size, (void*)image, sizeof(gfxcolor_t)*size, Z_BEST_SPEED);
+    writer_writeU32(w, compressdata_size);
+    w->write(w, filter, img->height);
+    w->write(w, compressdata, compressdata_size);
+    free(image);
+    free(filter);
+#else
+    w->write(w, img->data, img->width*img->height*sizeof(gfxcolor_t));
+#endif
+#ifdef STATS
+    state->size_images += w->pos - oldpos;
+#endif
+}
+static gfximage_t readImage(reader_t*r, state_t*state)
 {
     gfximage_t img;
     img.width = reader_readU16(r);
     img.height = reader_readU16(r);
-    img.data = (gfxcolor_t*)rfx_alloc(img.width*img.height*4);
-    r->read(r, img.data, img.width*img.height*4);
+    uLongf size = img.width*img.height*sizeof(gfxcolor_t);
+    img.data = malloc(size);
+#ifdef COMPRESS_IMAGES
+    uLongf compressdata_size = reader_readU32(r);
+    void*compressdata = malloc(compressdata_size);
+    unsigned char*filter = malloc(img.height);
+    r->read(r, filter, img.height);
+    r->read(r, compressdata, compressdata_size);
+    uncompress((void*)img.data, &size, compressdata, compressdata_size);
+    int y;
+    unsigned char*line = malloc(img.width*sizeof(gfxcolor_t));
+    for(y=0;y<img.height;y++) {
+       png_inverse_filter_32(filter[y], (void*)&img.data[y*img.width], 
+                             y?(void*)&img.data[(y-1)*img.width]:(void*)0, 
+                             line, img.width);
+       memcpy(&img.data[y*img.width], line, img.width*sizeof(gfxcolor_t));
+    }
+    free(filter);
+#else
+    r->read(r, img.data, size);
+#endif
     return img;
 }
-static gfxmatrix_t readMatrix(reader_t*r)
+
+static void dumpMatrix(writer_t*w, state_t*state, gfxmatrix_t*matrix)
+{
+    writer_writeDouble(w, matrix->m00);
+    writer_writeDouble(w, matrix->m01);
+    writer_writeDouble(w, matrix->m10);
+    writer_writeDouble(w, matrix->m11);
+    writer_writeDouble(w, matrix->tx);
+    writer_writeDouble(w, matrix->ty);
+#ifdef STATS
+    state->size_matrices += 6*8;
+#endif
+}
+static gfxmatrix_t readMatrix(reader_t*r, state_t*state)
 {
     gfxmatrix_t matrix;
     matrix.m00 = reader_readDouble(r);
@@ -167,7 +252,31 @@ static gfxmatrix_t readMatrix(reader_t*r)
     matrix.ty = reader_readDouble(r);
     return matrix;
 }
-static gfxcolor_t readColor(reader_t*r)
+static void dumpXY(writer_t*w, state_t*state, gfxmatrix_t*matrix)
+{
+    writer_writeDouble(w, matrix->tx);
+    writer_writeDouble(w, matrix->ty);
+#ifdef STATS
+    state->size_positions += 2*8;
+#endif
+}
+static void readXY(reader_t*r, state_t*state, gfxmatrix_t*m)
+{
+    m->tx = reader_readDouble(r);
+    m->ty = reader_readDouble(r);
+}
+
+static void dumpColor(writer_t*w, state_t*state, gfxcolor_t*color)
+{
+    writer_writeU8(w, color->r);
+    writer_writeU8(w, color->g);
+    writer_writeU8(w, color->b);
+    writer_writeU8(w, color->a);
+#ifdef STATS
+    state->size_colors += 4;
+#endif
+}
+static gfxcolor_t readColor(reader_t*r, state_t*state)
 {
     gfxcolor_t col;
     col.r = reader_readU8(r);
@@ -176,13 +285,18 @@ static gfxcolor_t readColor(reader_t*r)
     col.a = reader_readU8(r);
     return col;
 }
-static void readXY(reader_t*r, gfxmatrix_t*m)
+
+static void dumpGradient(writer_t*w, state_t*state, gfxgradient_t*gradient)
 {
-    m->tx = reader_readDouble(r);
-    m->ty = reader_readDouble(r);
+    while(gradient) {
+       writer_writeU8(w, 1);
+       dumpColor(w, state, &gradient->color);
+       writer_writeFloat(w, gradient->pos);
+       gradient = gradient->next;
+    }
+    writer_writeU8(w, 0);
 }
-
-static gfxgradient_t* readGradient(reader_t*r)
+static gfxgradient_t* readGradient(reader_t*r, state_t*state)
 {
     gfxgradient_t*start = 0, *pos = 0;
     while(1) {
@@ -196,62 +310,13 @@ static gfxgradient_t* readGradient(reader_t*r)
            pos->next = g;
            pos = g;
        }
-       g->color = readColor(r);
+       g->color = readColor(r, state);
        g->pos = reader_readFloat(r);
     }
     return start;
 }
-static gfxcxform_t* readCXForm(reader_t*r)
-{
-    U8 type = reader_readU8(r);
-    if(!type)
-       return 0;
-    gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
-    c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
-    c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
-    c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
-    c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
-    return c;
-}
 
-static void dumpColor(writer_t*w, gfxcolor_t*color)
-{
-    writer_writeU8(w, color->r);
-    writer_writeU8(w, color->g);
-    writer_writeU8(w, color->b);
-    writer_writeU8(w, color->a);
-}
-static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
-{
-    writer_writeDouble(w, matrix->m00);
-    writer_writeDouble(w, matrix->m01);
-    writer_writeDouble(w, matrix->m10);
-    writer_writeDouble(w, matrix->m11);
-    writer_writeDouble(w, matrix->tx);
-    writer_writeDouble(w, matrix->ty);
-}
-static void dumpXY(writer_t*w, gfxmatrix_t*matrix)
-{
-    writer_writeDouble(w, matrix->tx);
-    writer_writeDouble(w, matrix->ty);
-}
-static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
-{
-    while(gradient) {
-       writer_writeU8(w, 1);
-       dumpColor(w, &gradient->color);
-       writer_writeFloat(w, gradient->pos);
-       gradient = gradient->next;
-    }
-    writer_writeU8(w, 0);
-}
-static void dumpImage(writer_t*w, gfximage_t*image)
-{
-    writer_writeU16(w, image->width);
-    writer_writeU16(w, image->height);
-    w->write(w, image->data, image->width*image->height*4);
-}
-static void dumpCXForm(writer_t*w, gfxcxform_t*c)
+static void dumpCXForm(writer_t*w, state_t*state, gfxcxform_t*c)
 {
     if(!c) {
        writer_writeU8(w, 0);
@@ -263,8 +328,25 @@ static void dumpCXForm(writer_t*w, gfxcxform_t*c)
        writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
     }
 }
-static void dumpFont(writer_t*w, gfxfont_t*font)
+static gfxcxform_t* readCXForm(reader_t*r, state_t*state)
+{
+    U8 type = reader_readU8(r);
+    if(!type)
+       return 0;
+    gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
+    c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
+    c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
+    c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
+    c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
+    return c;
+}
+
+static void dumpFont(writer_t*w, state_t*state, gfxfont_t*font)
 {
+    int oldpos = w->pos;
+#ifdef STATS
+    int old_size_lines = state->size_lines;
+#endif
     writer_writeString(w, font->id);
     writer_writeU32(w, font->num_glyphs);
     writer_writeU32(w, font->max_unicode);
@@ -272,7 +354,7 @@ static void dumpFont(writer_t*w, gfxfont_t*font)
     writer_writeDouble(w, font->descent);
     int t;
     for(t=0;t<font->num_glyphs;t++) {
-       dumpLine(w, font->glyphs[t].line);
+       dumpLine(w, state, font->glyphs[t].line);
        writer_writeDouble(w, font->glyphs[t].advance);
        writer_writeU32(w, font->glyphs[t].unicode);
        if(font->glyphs[t].name) {
@@ -290,8 +372,12 @@ static void dumpFont(writer_t*w, gfxfont_t*font)
        writer_writeU32(w, font->kerning[t].c2);
        writer_writeU32(w, font->kerning[t].advance);
     }
+#ifdef STATS
+    state->size_lines = old_size_lines;
+    state->size_fonts += w->pos - oldpos;
+#endif
 }
-static gfxfont_t*readFont(reader_t*r)
+static gfxfont_t*readFont(reader_t*r, state_t*state)
 {
     gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
     font->id = reader_readString(r);
@@ -303,7 +389,7 @@ static gfxfont_t*readFont(reader_t*r)
     font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
     int t;
     for(t=0;t<font->num_glyphs;t++) {
-       font->glyphs[t].line = readLine(r);
+       font->glyphs[t].line = readLine(r, state);
        font->glyphs[t].advance = reader_readDouble(r);
        font->glyphs[t].unicode = reader_readU32(r);
        font->glyphs[t].name = reader_readString(r);
@@ -327,6 +413,53 @@ static gfxfont_t*readFont(reader_t*r)
     return font;
 }
 
+/* ----------------- reading/writing of primitives with caching -------------- */
+
+static char* read_string(reader_t*r, state_t*state, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST) {
+       assert(state->last_string[id]);
+       return strdup(state->last_string[id]);
+    }
+    char*s = reader_readString(r);
+    state->last_string[id] = strdup(s);
+    return s;
+}
+static gfxcolor_t read_color(reader_t*r, state_t*state, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST)
+       return state->last_color[id];
+    gfxcolor_t c = readColor(r, state);
+    state->last_color[id] = c;
+    return c;
+}
+static gfxmatrix_t read_matrix(reader_t*r, state_t*state, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST) {
+       gfxmatrix_t m = state->last_matrix[id];
+       readXY(r, state, &m);
+       return m;
+    }
+    gfxmatrix_t m = readMatrix(r, state);
+    state->last_matrix[id] = m;
+    return m;
+}
+
+/* --------------------------- record device operations ---------------------- */
+
+static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
+{
+    internal_t*i = (internal_t*)dev->internal;
+    msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
+    writer_writeU8(&i->w, OP_SETPARAM);
+    writer_writeString(&i->w, key);
+    writer_writeString(&i->w, value);
+    return 1;
+}
+
 static void record_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
 {
     internal_t*i = (internal_t*)dev->internal;
@@ -334,10 +467,10 @@ static void record_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t widt
     writer_writeU8(&i->w, OP_STROKE);
     writer_writeDouble(&i->w, width);
     writer_writeDouble(&i->w, miterLimit);
-    dumpColor(&i->w, color);
+    dumpColor(&i->w, &i->state, color);
     writer_writeU8(&i->w, cap_style);
     writer_writeU8(&i->w, joint_style);
-    dumpLine(&i->w, line);
+    dumpLine(&i->w, &i->state, line);
 }
 
 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
@@ -345,7 +478,7 @@ static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
     internal_t*i = (internal_t*)dev->internal;
     msg("<trace> record: %08x STARTCLIP\n", dev);
     writer_writeU8(&i->w, OP_STARTCLIP);
-    dumpLine(&i->w, line);
+    dumpLine(&i->w, &i->state, line);
     i->cliplevel++;
 }
 
@@ -365,8 +498,8 @@ static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
     internal_t*i = (internal_t*)dev->internal;
     msg("<trace> record: %08x FILL\n", dev);
     writer_writeU8(&i->w, OP_FILL);
-    dumpColor(&i->w, color);
-    dumpLine(&i->w, line);
+    dumpColor(&i->w, &i->state, color);
+    dumpLine(&i->w, &i->state, line);
 }
 
 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
@@ -374,10 +507,10 @@ static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*
     internal_t*i = (internal_t*)dev->internal;
     msg("<trace> record: %08x FILLBITMAP\n", dev);
     writer_writeU8(&i->w, OP_FILLBITMAP);
-    dumpImage(&i->w, img);
-    dumpMatrix(&i->w, matrix);
-    dumpLine(&i->w, line);
-    dumpCXForm(&i->w, cxform);
+    dumpImage(&i->w, &i->state, img);
+    dumpMatrix(&i->w, &i->state, matrix);
+    dumpLine(&i->w, &i->state, line);
+    dumpCXForm(&i->w, &i->state, cxform);
 }
 
 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
@@ -386,9 +519,9 @@ static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradie
     msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
     writer_writeU8(&i->w, OP_FILLGRADIENT);
     writer_writeU8(&i->w, type);
-    dumpGradient(&i->w, gradient);
-    dumpMatrix(&i->w, matrix);
-    dumpLine(&i->w, line);
+    dumpGradient(&i->w, &i->state, gradient);
+    dumpMatrix(&i->w, &i->state, matrix);
+    dumpLine(&i->w, &i->state, line);
 }
 
 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
@@ -397,7 +530,7 @@ static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
     msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
     if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
        writer_writeU8(&i->w, OP_ADDFONT);
-       dumpFont(&i->w, font);
+       dumpFont(&i->w, &i->state, font);
        i->fontlist = gfxfontlist_addfont(i->fontlist, font);
     }
 }
@@ -423,12 +556,19 @@ static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr,
 
     writer_writeU8(&i->w, OP_DRAWCHAR|flags);
     writer_writeU32(&i->w, glyphnr);
+#ifdef STATS
+    i->state.size_chars += 5;
+#endif
+
     if(!(flags&FLAG_SAME_AS_LAST)) {
        writer_writeString(&i->w, font_id);
-       dumpColor(&i->w, color);
-       dumpMatrix(&i->w, matrix);
+       dumpColor(&i->w, &i->state, color);
+       dumpMatrix(&i->w, &i->state, matrix);
+       i->state.last_string[OP_DRAWCHAR] = strdup(font_id);
+       i->state.last_color[OP_DRAWCHAR] = *color;
+       i->state.last_matrix[OP_DRAWCHAR] = *matrix;
     } else {
-       dumpXY(&i->w, matrix);
+       dumpXY(&i->w, &i->state, matrix);
     }
 }
 
@@ -453,42 +593,11 @@ static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*ac
     internal_t*i = (internal_t*)dev->internal;
     msg("<trace> record: %08x DRAWLINK\n", dev);
     writer_writeU8(&i->w, OP_DRAWLINK);
-    dumpLine(&i->w, line);
+    dumpLine(&i->w, &i->state, line);
     writer_writeString(&i->w, action);
 }
 
-static char* read_string(reader_t*r, state_t*state, U8 id, U8 flags)
-{
-    assert(id>=0 && id<16);
-    if(flags&FLAG_SAME_AS_LAST) {
-       assert(state->last_string[id]);
-       return state->last_string[id];
-    }
-    char*s = reader_readString(r);
-    state->last_string[id] = strdup(s);
-    return s;
-}
-static gfxcolor_t read_color(reader_t*r, state_t*s, U8 id, U8 flags)
-{
-    assert(id>=0 && id<16);
-    if(flags&FLAG_SAME_AS_LAST)
-       return s->last_color[id];
-    gfxcolor_t c = readColor(r);
-    s->last_color[id] = c;
-    return c;
-}
-static gfxmatrix_t read_matrix(reader_t*r, state_t*s, U8 id, U8 flags)
-{
-    assert(id>=0 && id<16);
-    if(flags&FLAG_SAME_AS_LAST) {
-       gfxmatrix_t m = s->last_matrix[id];
-       readXY(r, &m);
-       return m;
-    }
-    gfxmatrix_t m = readMatrix(r);
-    s->last_matrix[id] = m;
-    return m;
-}
+/* ------------------------------- replaying --------------------------------- */
 
 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
 {
@@ -543,7 +652,7 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
                msg("<trace> replay: STROKE");
                double width = reader_readDouble(r);
                double miterlimit = reader_readDouble(r);
-               gfxcolor_t color = readColor(r);
+               gfxcolor_t color = readColor(r, &state);
                gfx_capType captype;
                int v = reader_readU8(r);
                switch (v) {
@@ -558,14 +667,14 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
                    case 1: jointtype = gfx_joinRound; break;
                    case 2: jointtype = gfx_joinBevel; break;
                }
-               gfxline_t* line = readLine(r);
+               gfxline_t* line = readLine(r, &state);
                out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
                gfxline_free(line);
                break;
            }
            case OP_STARTCLIP: {
                msg("<trace> replay: STARTCLIP");
-               gfxline_t* line = readLine(r);
+               gfxline_t* line = readLine(r, &state);
                out->startclip(out, line);
                gfxline_free(line);
                break;
@@ -577,18 +686,18 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
            }
            case OP_FILL: {
                msg("<trace> replay: FILL");
-               gfxcolor_t color = readColor(r);
-               gfxline_t* line = readLine(r);
+               gfxcolor_t color = readColor(r, &state);
+               gfxline_t* line = readLine(r, &state);
                out->fill(out, line, &color);
                gfxline_free(line);
                break;
            }
            case OP_FILLBITMAP: {
                msg("<trace> replay: FILLBITMAP");
-               gfximage_t img = readImage(r);
-               gfxmatrix_t matrix = readMatrix(r);
-               gfxline_t* line = readLine(r);
-               gfxcxform_t* cxform = readCXForm(r);
+               gfximage_t img = readImage(r, &state);
+               gfxmatrix_t matrix = readMatrix(r, &state);
+               gfxline_t* line = readLine(r, &state);
+               gfxcxform_t* cxform = readCXForm(r, &state);
                out->fillbitmap(out, line, &img, &matrix, cxform);
                gfxline_free(line);
                if(cxform)
@@ -606,15 +715,15 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
                    case 1:
                      type = gfxgradient_linear; break;
                }  
-               gfxgradient_t*gradient = readGradient(r);
-               gfxmatrix_t matrix = readMatrix(r);
-               gfxline_t* line = readLine(r);
+               gfxgradient_t*gradient = readGradient(r, &state);
+               gfxmatrix_t matrix = readMatrix(r, &state);
+               gfxline_t* line = readLine(r, &state);
                out->fillgradient(out, line, gradient, type, &matrix);
                break;
            }
            case OP_DRAWLINK: {
                msg("<trace> replay: DRAWLINK");
-               gfxline_t* line = readLine(r);
+               gfxline_t* line = readLine(r, &state);
                char* s = reader_readString(r);
                out->drawlink(out,line,s);
                gfxline_free(line);
@@ -623,7 +732,7 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
            }
            case OP_ADDFONT: {
                msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
-               gfxfont_t*font = readFont(r);
+               gfxfont_t*font = readFont(r, &state);
                fontlist = gfxfontlist_addfont(fontlist, font);
                out->addfont(out, font);
                break;
@@ -766,6 +875,22 @@ static gfxresult_t* record_finish(struct _gfxdevice*dev)
     if(i->cliplevel) {
        msg("<error> Warning: unclosed cliplevels");
     }
+
+#ifdef STATS
+    int total = i->w.pos;
+    if(total && i->use_tempfile) {
+       state_t*s = &i->state;
+       msg("<notice> record device finished. stats:");
+       msg("<notice> %4.1f%% matrices (%d bytes)", s->size_matrices*100.0/total, s->size_matrices);
+       msg("<notice> %4.1f%% positions (%d bytes)", s->size_positions*100.0/total, s->size_positions);
+       msg("<notice> %4.1f%% colors (%d bytes)", s->size_colors*100.0/total, s->size_colors);
+       msg("<notice> %4.1f%% lines (%d bytes)", s->size_lines*100.0/total, s->size_lines);
+       msg("<notice> %4.1f%% fonts (%d bytes)", s->size_fonts*100.0/total, s->size_fonts);
+       msg("<notice> %4.1f%% images (%d bytes)", s->size_images*100.0/total, s->size_images);
+       msg("<notice> %4.1f%% characters (%d bytes)", s->size_chars*100.0/total, s->size_chars);
+       msg("<notice> total: %d bytes", total);
+    }
+#endif
     
     writer_writeU8(&i->w, OP_END);
     
index 7190553..9ab0325 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -358,7 +358,7 @@ static void applyfilter3(int mode, unsigned char*src, unsigned char*old, unsigne
     }    
 }
 
-static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
+void png_inverse_filter_32(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
 {
     int x;
     unsigned char lastr=0;
@@ -441,7 +441,6 @@ static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old,
     }    
 }
 
-
 EXPORT int getPNGdimensions(const char*sname, int*destwidth, int*destheight)
 {
     FILE*fi;
@@ -649,7 +648,7 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
                old = &data2[(y-1)*header.width*4];
            }
            if(header.mode == 6) { 
-               applyfilter4(mode, src, old, dest, header.width);
+               png_inverse_filter_32(mode, src, old, dest, header.width);
            } else { // header.mode = 2
                applyfilter3(mode, src, old, dest, header.width);
                /* replace alpha color */
index 775a84f..aa4b9c5 100644 (file)
--- a/lib/png.h
+++ b/lib/png.h
@@ -23,6 +23,9 @@
 extern "C" {
 #endif
 
+int png_apply_filter_32(unsigned char*dest, unsigned char*src, int width, int y);
+void png_inverse_filter_32(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width);
+
 int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char**destdata);
 int getPNGdimensions(const char*sname, int*destwidth, int*destheight);