static gfximage_t readImage(reader_t*r, state_t*state)
{
    gfximage_t img;
    img.width = reader_readU16(r);
    img.height = reader_readU16(r);
    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);
# ifdef FILTER_IMAGES
    unsigned char*filter = malloc(img.height);
    r->read(r, filter, img.height);
# endif
    r->read(r, compressdata, compressdata_size);
   
# ifdef HAVE_FASTLZ
    fastlz_decompress(compressdata, compressdata_size, (void*)img.data, size);
# else
    uncompress((void*)img.data, &size, compressdata, compressdata_size);
# endif
    free(compressdata);

# ifdef FILTER_IMAGES
    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(line);
    free(filter);
# endif

#else
    r->read(r, img.data, size);
#endif
    return img;
}
Example #2
0
static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r, gfxfontlist_t**fontlist)
{
    internal_t*i = 0;
    if(dev) {
	i = (internal_t*)dev->internal;
    }
    gfxfontlist_t*_fontlist=0;
    if(!fontlist) {
	fontlist = &_fontlist;
    }

    state_t state;
    memset(&state, 0, sizeof(state));

    while(1) {
	unsigned char op;
	if(r->read(r, &op, 1)!=1)
	    break;
	unsigned char flags = op&0xf0;
	op&=0x0f;

	switch(op) {
	    case OP_END:
		goto finish;
	    case OP_SETPARAM: {
		msg("<trace> replay: SETPARAM");
		char*key;
		char*value;
		key = reader_readString(r);
		value = reader_readString(r);
		out->setparameter(out, key, value);
		free(key);
		free(value);
		break;
	    }
	    case OP_STARTPAGE: {
		msg("<trace> replay: STARTPAGE");
		U16 width = reader_readU16(r);
		U16 height = reader_readU16(r);
		out->startpage(out, width, height);
		break;
	    }
	    case OP_ENDPAGE: {
		msg("<trace> replay: ENDPAGE");
		out->endpage(out);
		break;
	    }
	    case OP_FINISH: {
		msg("<trace> replay: FINISH");
		break;
	    }
	    case OP_STROKE: {
		msg("<trace> replay: STROKE");
		double width = reader_readDouble(r);
		double miterlimit = reader_readDouble(r);
		gfxcolor_t color = readColor(r, &state);
		gfx_capType captype;
		int v = reader_readU8(r);
		switch (v) {
		    case 0: captype = gfx_capButt; break;
		    case 1: captype = gfx_capRound; break;
		    case 2: captype = gfx_capSquare; break;
		}
		gfx_joinType jointtype;
		v = reader_readU8(r);
		switch (v) {
		    case 0: jointtype = gfx_joinMiter; break;
		    case 1: jointtype = gfx_joinRound; break;
		    case 2: jointtype = gfx_joinBevel; break;
		}
		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, &state);
		out->startclip(out, line);
		gfxline_free(line);
		break;
	    }
	    case OP_ENDCLIP: {
		msg("<trace> replay: ENDCLIP");
		out->endclip(out);
		break;
	    }
	    case OP_FILL: {
		msg("<trace> replay: FILL");
		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, &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)
		    free(cxform);
		free(img.data);img.data=0;
		break;
	    }
	    case OP_FILLGRADIENT: {
		msg("<trace> replay: FILLGRADIENT");
		gfxgradienttype_t type;
		int v = reader_readU8(r);
		switch (v) {
		    case 0: 
		      type = gfxgradient_radial; break;
		    case 1:
		      type = gfxgradient_linear; break;
		}  
		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, &state);
		char* s = reader_readString(r);
		out->drawlink(out,line,s);
		gfxline_free(line);
		free(s);
		break;
	    }
	    case OP_ADDFONT: {
		msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
		gfxfont_t*font = readFont(r, &state);
		if(!gfxfontlist_hasfont(*fontlist, font)) {
		    *fontlist = gfxfontlist_addfont(*fontlist, font);
		    out->addfont(out, font);
		} else {
		    gfxfont_free(font);
		}
		break;
	    }
	    case OP_DRAWCHAR: {
		U32 glyph = reader_readU32(r);
		gfxmatrix_t m = {1,0,0, 0,1,0};
		char* id = 0;
		if(!(flags&FLAG_ZERO_FONT))
		    id = read_string(r, &state, op, flags);
		gfxcolor_t color = read_color(r, &state, op, flags);
		gfxmatrix_t matrix = read_matrix(r, &state, op, flags);

		gfxfont_t*font = id?gfxfontlist_findfont(*fontlist, id):0;
		if(i && !font) {
		    font = gfxfontlist_findfont(i->fontlist, id);
		}
		msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
		out->drawchar(out, font, glyph, &color, &matrix);
		if(id)
		    free(id);
		break;
	    }
	}
    }
finish:
    state_clear(&state);
    r->dealloc(r);
    if(_fontlist)
	gfxfontlist_free(_fontlist, 0);
}