shithub: jbig2

ref: a6820e140bc4b306fb7852ae4994bf6d38f2a906
dir: /jbig2_image.c/

View raw version
/*
    jbig2dec
    
    Copyright (c) 2001-2002 artofcode LLC.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    $Id: jbig2_image.c,v 1.12 2002/07/03 19:43:21 giles Exp $
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_image.h"


/* allocate a Jbig2Image structure and its associated bitmap */
Jbig2Image* jbig2_image_new(Jbig2Ctx *ctx, int width, int height)
{
	Jbig2Image	*image;
	int		stride;
	
	image = (Jbig2Image *)jbig2_alloc(ctx->allocator, sizeof(*image));
	if (image == NULL) {
		jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
			       "could not allocate image structure");
		return NULL;
	}
	
	stride = ((width - 1) >> 3) + 1; /* generate a byte-aligned stride */
	image->data = (uint8_t *)jbig2_alloc(ctx->allocator, stride*height);
	if (image->data == NULL) {
                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
                    "could not allocate image data buffer! [%d bytes]\n", stride*height);
		jbig2_free(ctx->allocator, image);
		return NULL;
	}
	
	image->width = width;
	image->height = height;
	image->stride = stride;
	
	return image;
}


/* free a Jbig2Image structure and its associated memory */
void jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
{
	jbig2_free(ctx->allocator, image->data);
	jbig2_free(ctx->allocator, image);
}

/* composite one jbig2_image onto another */
int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, 
			int x, int y, Jbig2ComposeOp op)
{
    int w, h;
    int leftbyte, rightbyte;
    int leftbits, rightbits;
    int i, j;
    uint8_t *s, *ss;
    uint8_t *d, *dd;
    uint8_t mask, highmask;
    
    if (op != JBIG2_COMPOSE_OR) {
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
            "non-OR composition modes NYI");
    }

    /* clip */
    // FIXME: this isn't sufficient
    w = src->width;
    h = src->height;
    if (x < 0) { w += x; x = 0; }
    if (y < 0) { h += y; y = 0; } 
    w = (x + w < dst->width) ? w : dst->width - x;
    h = (y + h < dst->height) ? h : dst->height - y;
#ifdef DEBUG    
    fprintf(stderr, "composting %dx%d at (%d, %d) afer clipping\n",
        w, h, x, y);
#endif
    
    /* special case complete/strip replacement */
    if ((x == 0) && (w == src->width)) {
        memcpy(dst->data + y*dst->stride, src->data, h*src->stride);
        return 0;
    }

    /* general OR case */
    leftbyte = x >> 3;
    leftbits = x & 7;
    rightbyte = (x + w) >> 3;
    rightbits = (x + w) & 7;
    fprintf(stderr, "left byte:bits %d:%d right byte:bits %d:%d\n",
        leftbyte, leftbits, rightbyte, rightbits);

    s = ss = src->data;
    d = dd = dst->data + y*dst->stride + leftbyte;    
    if (leftbyte == rightbyte) {
        mask = (1 << (w & 7)) - 1;
        for (j = 0; j < h; j++) {
            *d |= (*s & mask) << leftbits;
            d += dst->stride;
            s += src->stride;
        }
    } else {
        mask = 1 << (8 - leftbits) - 1;
        for (j = 0; j < h; j++) {
            *d++ |= (*s & mask) << leftbits;
            for(i = leftbyte; i < rightbyte; i++) {
                *d |= (*s++ & ~mask) >> leftbits;
                *d++ |= (*s & mask) << leftbits;
            }
            *d |= (*++s & highmask) << (8 - rightbits);
            d = (dd += dst->stride);
            s = (ss += src->stride);
        }
    }
            
    return 0;
}