summaryrefslogtreecommitdiffstats
path: root/chalk/colorspaces/wetsticky/ws/load_ppm.c
blob: 6368ee083aa0e8855e4655a471e29a1e80256dd8 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
	FILE:		load_ppm.c
	PURPOSE:	Defines the routines to load a PPM portable pixmap image file.
	AUTHOR:		David England
	VERSION:	1.00  (10-May-91)


Copyright 1991, 1992, 2002, 2003 Tunde Cockshott, Kevin Waite, David England. 

Contact David England d.england@livjm.ac.uk
School of Computing and Maths Sciences,
Liverpool John Moores University 
Liverpool L3 3AF
United Kingdom
Phone +44 151 231 2271

Wet and Sticky 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. Wet and Sticky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Wet and Sticky; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

*/

#include "constants.h"
#include "types.h"
#include "canvas.h"

#include <ctype.h>
#include <ppm.h>
#include <ppmcmap.h>

/* Max number of colors allowed in ppm input. */
#define MAXCOLORS 256

extern CELL canvas[CANVAS_WIDTH][CANVAS_HEIGHT];

float
max_rgb(r,g,b)
float r;
float g;
float b;
{
	if ((r > g) && (r > b))
		return (r);

	if ((b > g) && (b > r))
		return (b);

	if ((g > b) && (g > r))
		return (g);

	return(0);
}

float 
min_rgb(r,g,b) 
float r; 
float g; 
float b;
{ 
        if ((r < g) && (b < g)) 
                return (r); 
 
        if ((b < g) && (b < r))
                return (b); 

        if ((g < b) && (g < r)) 
                return (g); 

	return(0);
}


int
GetHue(red, green, blue) /* rgb to hls */
int red;
int green;
int blue;
{
	float min_col, max_col;
	float h,s,l;
	float rc, gc, bc;
	float r, g, b;

	r =  (float)red/255.0;
	b =  (float)green/255.0;
	g =  (float)blue/255.0;

	max_col = (float)max_rgb(r, g, b);
	min_col = (float)min_rgb(r, g, b);

	l = (max_col + min_col)/2.0 ;

	if ( max_col == min_col) {
		s = 0.0;
		h = 0.0;
	} else  {
		if ( l < 0.5) {
			s = (max_col - min_col)/(max_col + min_col);
		} else  s = (max_col - min_col)/(2 - max_col - min_col);

		rc = (max_col -r)/( max_col - min_col);
		gc = (max_col -g)/(max_col - min_col);
		bc = (max_col -b)/(max_col - min_col);

		if (r == max_col)
			h = bc - gc;
		else if (g == max_col )
			h = 2 + rc - bc;
			else if (b == max_col)
				h = 4  + gc -rc;
		
		h = h * 60;

		if ( h < 0.0)
			h = h + 360;
	}

	return ((int)h);


}

GetHuePaint(r,g,b)
int r;
int g;
int b;
{

	/*if ((r == 0) && (g == 0) && (b ==0)) {
		canvas[i][j].contents.liquid_content = 0;
		   canvas[i][j].contents.drying_rate = 0;
		   canvas[i][j].contents.miscibility = 0;

		   canvas[i][j].contents.colour.hue = 0;
		   canvas[i][j].contents.colour.saturation = 1.0;
		   canvas[i][j].contents.colour.lightness = 0.0;
		   canvas[i][j].volume = 80;
		return (0);
	}

	if ((r == 255) && (g == 255) && ( b == 255)) {
		canvas[i][j].contents.liquid_content = 0;
                   canvas[i][j].contents.drying_rate = 0;
                   canvas[i][j].contents.miscibility = 0;

                   canvas[i][j].contents.colour.hue = 128;
                   canvas[i][j].contents.colour.saturation = 1.0;
                   canvas[i][j].contents.colour.lightness = 0.0;
                   canvas[i][j].volume = 80;
		return (128);
	}*/
}

load_ppm_format(filename, width, height)
char *filename;
int *width;
int *height;
{

    FILE* ifp;
    pixel **pixels;
    int rows, cols, i, j;
    pixval maxval;
	int red, green, blue;
	int hue;

    /*ppm_init( &argc, argv );*/



   ifp = pm_openr( filename);

    pixels = ppm_readppm( ifp, &cols, &rows, &maxval );

	*(width) = cols;
	*(height) = rows;

	fprintf(stderr,"Loading file %s, %dx%d. Please wait ", filename, rows, 
cols);

	if (rows > CANVAS_HEIGHT)
		rows = CANVAS_HEIGHT;

	if (cols > CANVAS_WIDTH)
		cols = CANVAS_WIDTH;

	for (i=0; i< rows; i++)  {
		for (j=0; j< cols; j++) {
			red = PPM_GETR(pixels[i][j]);
			green = PPM_GETG(pixels[i][j]);
			blue = PPM_GETB(pixels[i][j]);
			
			/*hue = GetHue(red, green, blue);*/

			/* For gray scale only */

			hue = 255 - red;


			/*fprintf(stderr,"hue %d ", hue);*/
			/*GetHuePaint(red, green, blue, i, j);*/

			canvas[i][j].contents.liquid_content = 80;
			   canvas[i][j].contents.drying_rate = 80;
			   canvas[i][j].contents.miscibility = 80;

			   canvas[i][j].contents.colour.hue = hue;
			   canvas[i][j].contents.colour.saturation = 1.0;
			   canvas[i][j].contents.colour.lightness = 0.0;
			   canvas[i][j].volume = (float)hue/2.5;

			if (red == 0) 
			  if  (green == 0) 
			   if  ( blue == 0) {
				canvas[i][j].contents.liquid_content = 0;
				canvas[i][j].contents.drying_rate = 0;
                           canvas[i][j].contents.miscibility = 0;
				canvas[i][j].contents.colour.hue = 0;
				   canvas[i][j].volume = 0;
			}

			if (red == 255) 
			   if (green == 255) 
			      if ( blue == 255) {
                                canvas[i][j].contents.liquid_content = 0;
				canvas[i][j].contents.drying_rate = 0;
                                canvas[i][j].contents.miscibility = 0;
                                canvas[i][j].contents.colour.hue = 360;
				   canvas[i][j].volume = 0;
                        }


		}		
		if (( i %10) == 0){
			fprintf(stderr,".");
			fflush(stderr);
		}
	}

	printf(" done\n");

    pm_close( ifp );

    /*exit(0);*/

}