File: jpeg_image.C

package info (click to toggle)
xjig 2.4-13
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 476 kB
  • ctags: 1,499
  • sloc: cpp: 4,887; makefile: 1,187; perl: 11; sh: 6
file content (98 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <jpeglib.h>

#include "jpeg_image.H"

#undef XJIG_JPEG_DEBUG

jpegImage::jpegImage(const char *filename, int autocrop )
	: Image(filename, autocrop)
{
	LoadImageFile( filename );
	if (autocrop)	CropImage();
}

/*****************************/
int jpegImage::LoadImageFile(FILE *fp)
/*****************************/
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	int i, j, copied_lines;

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);
	jpeg_stdio_src(&cinfo, fp);
	jpeg_read_header(&cinfo, TRUE);

	/* Ask for palettized RGB output */
	cinfo.out_color_space = JCS_RGB;
	cinfo.quantize_colors = TRUE;

	jpeg_start_decompress(&cinfo);

	width=cinfo.output_width;
	height=cinfo.output_height;

#ifdef XJIG_JPEG_DEBUG
	fprintf(stderr, "jpeg width=%d height=%d\n", width, height);
	if(cinfo.output_components!=1) {
		fprintf(stderr, "Not getting palettized output from libjpeg!\n");
		exit(1);
	}
#endif

	data=(byte *)malloc(width*height*sizeof *data);
	if(!data) {
		fprintf(stderr, "Not enough memory to store jpeg data");
		exit(1);
	}

	JSAMPARRAY lines=(JSAMPARRAY)malloc(cinfo.rec_outbuf_height*sizeof *lines);
	if(!lines) {
		fprintf(stderr, "Not enough memory to for temporary jpeg buffer");
		exit(1);
	}
	for(i=0 ; i<cinfo.rec_outbuf_height ; ++i) {
		lines[i]=(JSAMPROW)malloc(width*sizeof *lines[i]);
		if(!lines[i]) {
			fprintf(stderr, "Not enough memory to for temporary jpeg buffer");
			exit(1);
		}
	}

	copied_lines=0;
	while(cinfo.output_scanline < cinfo.output_height) {
		int gotlines=jpeg_read_scanlines(&cinfo, lines, cinfo.rec_outbuf_height);
#ifdef XJIG_JPEG_DEBUG
		fprintf(stderr, "got %d lines from jpeg\n", gotlines);
#endif
		for(i=0;i<gotlines;++i)
			for(j=0;j<width;++j)
				data[(copied_lines+i)*width+j]=lines[i][j];
		copied_lines+=i;
#ifdef XJIG_JPEG_DEBUG
		fprintf(stderr, "have now filled in %d lines of data\n", copied_lines);
#endif
	}

	for(i=0 ; i<cinfo.rec_outbuf_height ; ++i)
		free(lines[i]);
	free(lines);

	HasColormap=1;
	ColorMapSize=cinfo.actual_number_of_colors;
	for(i=0;i<ColorMapSize;++i) {
		Red[i]   = cinfo.colormap[0][i];
		Green[i] = cinfo.colormap[1][i];
		Blue[i]  = cinfo.colormap[2][i];
	}
	Background=-1;

	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	return 0;
}