File: ppm_image.C

package info (click to toggle)
xjig 2.4-14.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 892 kB
  • sloc: cpp: 10,177; makefile: 1,142; perl: 23
file content (120 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (4)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern "C" {
#include <ppm.h>
#include <ppmcmap.h>
}

#include "ppm_image.H"

#undef XJIG_PPM_DEBUG

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

/*****************************/
int ppmImage::LoadImageFile(FILE *fp)
/*****************************/
{
	pixval maxval;
	pixel **ppmdata;
	int ncolors, i, j;
	colorhist_vector colors;
	colorhash_table hash;
	int fake_argc=1;
	char *fake_argv[]={"xjig-ppm-loader", 0};

	ppm_init(&fake_argc, fake_argv);
	ppmdata=ppm_readppm(fp, &width, &height, &maxval);
#ifdef XJIG_PPM_DEBUG
	fprintf(stderr, "ppm width=%d height=%d maxval=%d\n",
			  width, height, (int)maxval);
#endif

	/* Scale the color values up or down to 24 bits (RGB=888), which is what
	 * the xjig core expects. */
	if(maxval!=255)
		for(i=0;i<height;++i)
			for(j=0;j<width;++j)
				PPM_DEPTH(ppmdata[i][j], ppmdata[i][j], maxval, 255);

	/* Find out how many distinct colors are in the image */
	colors=ppm_computecolorhist(ppmdata, width, height, 0, &ncolors);
#ifdef XJIG_PPM_DEBUG
	fprintf(stderr, "ppm ncolors=%d\n", ncolors);
#endif

	HasColormap=1;

	if(ncolors>256) {
		fprintf(stderr,
				  "ppm image contains %d colors\n"
				  "Stupid Color Reduction activated.\n"
				  "For better quality, use ppmquant to reduce colors.\n",
				  ncolors);

		/* Stupid Color Reduction is an RGB=332 colormap. */
		int r, g, b;
		i=0;
		for(r=0;r<8;++r)
			for(g=0;g<8;++g)
				for(b=0;b<4;++b) {
					Red[i] = r*32;
					Green[i] = g*32;
					Blue[i] = b*64;
					++i;
				}

		ColorMapSize=256;

		data=(byte *)malloc(width*height*sizeof *data);
		if(!data) {
			fprintf(stderr, "Not enough memory to store ppm data");
			exit(1);
		}
		for(i=0;i<height;++i)
			for(j=0;j<width;++j) {
				r = PPM_GETR(ppmdata[i][j]);
				g = PPM_GETG(ppmdata[i][j]);
				b = PPM_GETB(ppmdata[i][j]);
				data[i*width+j]=(r & 0xe0) | ((g & 0xe0)>>3) | (b>>6);
			}
	} else {
		ColorMapSize=ncolors;

		hash=ppm_colorhisttocolorhash(colors, ncolors);
		for(i=0;i<ncolors;++i) {
			Red[i]   = PPM_GETR(colors[i].color);
			Green[i] = PPM_GETG(colors[i].color);
			Blue[i]  = PPM_GETB(colors[i].color);
#ifdef XJIG_PPM_DEBUG
			fprintf(stderr, "ppm color:%d value:%d hvalue:%d",
					  i, colors[i].value,
					  ppm_lookupcolor(hash, &colors[i].color));
			fprintf(stderr, " R:%d G:%d B:%d\n",
					  (int)Red[i], (int)Green[i], (int)Blue[i]);
#endif
		}

		data=(byte *)malloc(width*height*sizeof *data);
		if(!data) {
			fprintf(stderr, "Not enough memory to store ppm data");
			exit(1);
		}
		for(i=0;i<height;++i)
			for(j=0;j<width;++j)
				data[i*width+j]=ppm_lookupcolor(hash, &ppmdata[i][j]);

		ppm_freecolorhash(hash);
	}
	ppm_freecolorhist(colors);
	ppm_freearray(ppmdata, height);

	DefaultBackground();
	return 0;
}