Example #1
0
/* ############################################################################
Name           : load_pgm
Description    : Read PGM binary file (needs to free the return pointer).Load 
                 the image data in memory.

Arguments      Type                 Description
============== ===================  ===========================================
filename(IN)   unsigned char *      Filename of the image file.
width(OUT)     int *                Width of the image.
height(OUT)    int *                Height of the image.

Return Values                       Description
==================================  ===========================================
data                                The image data.

Globals        Type                 Description
============== ===================  ===========================================

Locals         Type                 Description
============== ===================  ===========================================
data           unsigned char *      The image data.
max            int                  The maximum value of the colour components 
                                    for the pixels.
w              int                  The width of the image.
h              int                  The height of the image.
c              size_t               The total number of elements successfully 
                                    read from image file.

############################################################################ */
unsigned char *load_pgm(const char *filename, int *width, int *height)
{
	unsigned char *data = NULL;
	int max, w, h;
	size_t c;
	
	FILE *f = fopen(filename, "rb");
	if (!f) return NULL;

	if (read_pnm_header(f, &w, &h, &max) != 5) {
		fclose(f);
		return NULL;
	}

	data = (unsigned char *)malloc(w*h);
	if (data == NULL){
		printf("Cannot allocate %d bytes for memory.\n", (w*h));
		exit(-1);
		}
	c = fread(data, 1, w*h, f);

	if (c != w*h) {
		free(data);
		data = NULL;
	}

	*width  = w;
	*height = h;
	
	return data;
}
Example #2
0
static int do_page(deark *c, lctx *d, int pagenum, de_int64 pos1)
{
	struct page_ctx *pg = NULL;
	int retval = 0;

	pg = de_malloc(c, sizeof(struct page_ctx));

	pg->fmt = identify_fmt(c, pos1);
	d->last_fmt = pg->fmt;
	pg->fmt_name = get_fmt_name(pg->fmt);
	if(pg->fmt==0) {
		de_err(c, "Not PNM/PAM format\n");
		goto done;
	}

	if(pagenum==0) {
		de_declare_fmt(c, pg->fmt_name);
	}

	if(pg->fmt==FMT_PAM) {
		if(!read_pam_header(c, d, pg, pos1)) goto done;
	}
	else {
		if(!read_pnm_header(c, d, pg, pos1)) goto done;
	}

	if(!do_image(c, d, pg, pg->hdr_parse_pos)) {
		goto done;
	}

	d->last_bytesused = (pg->hdr_parse_pos + pg->image_data_len) - pos1;

	retval = 1;
done:
	de_free(c, pg);
	return retval;
}