shithub: pdffs

ref: ee8ac152c7ed0eb09e087212a4e521bb448c7831
dir: pdffs/f_jbig2.c

View raw version
#include <u.h>
#include <libc.h>
#include "pdf.h"

/* 7.4.7 JBIG2Decode filter */

static u8int bh[] = {
	/* id string  */ 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a,
	/* sequential */ 0x01,
	/* one page   */ 0x00, 0x00, 0x00, 0x01,
};

static void *
memimage(Buffer *b)
{
	char *argv[] = {"jbig2", nil};
	return buf2memimage(b, "/bin/jbig2", argv);
}

static int
flreadall(void *aux, Buffer *bi, Buffer *bo)
{
	Stream *s;

	s = aux;
	bufput(bo, bh, sizeof(bh));
	if(s != nil)
		bufput(bo, s->buf.b, s->buf.sz);
	bufput(bo, bi->b, bi->sz);
	bi->off = bi->sz;
	bo->memimage = memimage;

	return 0;
}

static int
flopen(Filter *f, Object *o)
{
	Object *parms, *so;
	Stream *s;

	parms = dictget(o, "DecodeParms");
	s = nil;
	if((so = dictget(parms, "JBIG2Globals")) != &null && (s = Sopen(so)) == nil)
		return -1;
	f->aux = s;

	return 0;
}

static void
flclose(Filter *f)
{
	Sclose(f->aux);
}

Filter filterJBIG2 = {
	.name = "JBIG2Decode",
	.readall = flreadall,
	.open = flopen,
	.close = flclose,
};