summaryrefslogtreecommitdiff
path: root/camlibs/polaroid/jd350e.c
blob: d5d165e48928bc443a77c99442b67fd83b2a6761 (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
/* jd350e.c
 *
 * White balancing and brightness correction for the Jenoptik 
 * JD350 entrance camera
 *
 * Copyright © 2001 Michael Trawny <trawny99@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"

#include <stdlib.h>
#include <string.h>

#include <gphoto2/gphoto2-library.h>
#include <gphoto2/gphoto2-port-log.h>

#include "jd350e.h"
#include "jd350e_red.h"
/*#include "jd350e_blue.h"*/

#define GP_MODULE "jd350e"

#define THRESHOLD 0xf8

#define RED(p,x,y,w) *((p)+3*((y)*(w)+(x))  )
#define GREEN(p,x,y,w) *((p)+3*((y)*(w)+(x))+1)
#define BLUE(p,x,y,w) *((p)+3*((y)*(w)+(x))+2)

#define SWAP(a,b) {unsigned char t=(a); (a)=(b); (b)=t;}

#define MINMAX(a,min,max) { (min)=MIN(min,a); (max)=MAX(max,a); }

#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif


int jd350e_postprocessing(int width, int height, unsigned char* rgb){
	int
		x,y,
		red_min=255, red_max=0, 
		blue_min=255, blue_max=0, 
		green_min=255, green_max=0;
	double
		min, max, amplify;

	/* reverse image row by row... */

	for( y=0; y<height; y++){
		for( x=0; x<width/2; x++ ){
			SWAP( RED(rgb,x,y,width), RED(rgb,width-x-1,y,width));
			SWAP( GREEN(rgb,x,y,width), GREEN(rgb,width-x-1,y,width));
			SWAP( BLUE(rgb,x,y,width), BLUE(rgb,width-x-1,y,width));
		}
	}

	/* determine min and max per color... */

	for( y=0; y<height; y++){
		for( x=0; x<width; x++ ){
			MINMAX( RED(rgb,x,y,width), red_min,   red_max  );
			MINMAX( GREEN(rgb,x,y,width), green_min, green_max);
			MINMAX( BLUE(rgb,x,y,width), blue_min,  blue_max );
		}
	}

	/* white balancing ...                               */
	/* here is still some work to do: either blue or red */
	/* pixles need to be emphasized.                     */
	/* but how can the driver decide?                    */

#if 0
	if( (green_max+blue_max)/2 > red_max ){
#endif
		/* outdoor daylight : red color correction curve*/
		GP_DEBUG( "daylight mode");
		for( y=0; y<height; y++){
			for( x=0; x<width; x++ ){
				RED(rgb,x,y,width) = jd350e_red_curve[ RED(rgb,x,y,width) ];
			/* RED(rgb,x,y,width) = MIN(2*(unsigned)RED(rgb,x,y,width),255); */
			}
		}
		red_min = jd350e_red_curve[ red_min ];
		red_max = jd350e_red_curve[ red_max ];
		/* red_min = MIN(2*(unsigned)red_min,255); */
		/* red_max = MIN(2*(unsigned)red_max,255); */
#if 0
	}
	else if( (green_max+red_max)/2 > blue_max ){
		/* indoor electric light */
		GP_DEBUG( "electric light mode");
		for( y=0; y<height; y++){
			for( x=0; x<width; x++ ){
				BLUE(rgb,x,y,width) = MIN(2*(unsigned)BLUE(rgb,x,y,width),255);
			}
		}
		blue_min = MIN(2*(unsigned)blue_min,255);
		blue_max = MIN(2*(unsigned)blue_max,255);
	}
#endif

	/* Normalize brightness ... */

	max = MAX( MAX( red_max, green_max ), blue_max);
	min = MIN( MIN( red_min, green_min ), blue_min);
	amplify = 255.0/(max-min);

	for( y=0; y<height; y++){
		for( x=0; x<width; x++ ){
			RED(rgb,x,y,width)= MIN(amplify*(double)(RED(rgb,x,y,width)-min),255);
			GREEN(rgb,x,y,width)= MIN(amplify*(double)(GREEN(rgb,x,y,width)-min),255);
			BLUE(rgb,x,y,width)= MIN(amplify*(double)(BLUE(rgb,x,y,width)-min),255);
		}
	}

	return GP_OK;
}

int jd350e_postprocessing_and_flip(int width, int height, unsigned char* rgb){
	char *tmpline;
	int y, ret;

	ret = jd350e_postprocessing (width,height,rgb);
	if (ret < GP_OK)
		return ret;
	tmpline = malloc(width*3);
	if (!tmpline)
		return GP_ERROR_NO_MEMORY;
	for( y=0; y<height/2; y++){
		memcpy (tmpline, rgb+y*width*3, width*3);
		memcpy (rgb+y*width*3, rgb+(height-y-1)*width*3, width*3);
		memcpy (rgb+(height-y-1)*width*3, tmpline, width*3);
	}
	free(tmpline);
	return GP_OK;
}

int trust350fs_postprocessing(int width, int height, unsigned char* rgb) {
	int		i,x,y,min=255,max=0;
	double		amplify;
	unsigned char	*buf;
	const int	brightness_adjust = 16;
	
	/* flip horizontal */
#define RED(p,x,y,w) *((p)+3*((y)*(w)+(x))  )
#define GREEN(p,x,y,w) *((p)+3*((y)*(w)+(x))+1)
#define BLUE(p,x,y,w) *((p)+3*((y)*(w)+(x))+2)

#define SWAP(a,b) {unsigned char t=(a); (a)=(b); (b)=t;}

	for( y=0; y<height; y++){
		for( x=0; x<width/2; x++ ){
			SWAP( RED(rgb,x,y,width), RED(rgb,width-x-1,y,width));
			SWAP( GREEN(rgb,x,y,width), GREEN(rgb,width-x-1,y,width));
			SWAP( BLUE(rgb,x,y,width), BLUE(rgb,width-x-1,y,width));
		}
	}
	
	/* flip vertical */
	buf = malloc(width*3);
	if (!buf) return GP_ERROR_NO_MEMORY;
	for (i=0;i<height/2;i++) {
		memcpy(buf,rgb+i*width*3,width*3);
		memcpy(rgb+i*width*3,rgb+(height-i-1)*width*3,width*3);
		memcpy(rgb+(height-i-1)*width*3,buf,width*3);
	}
	free(buf);
	
	/* Normalize & adjust brightness ... */
#define MINMAX(a,min,max) { (min)=MIN(min,a); (max)=MAX(max,a); }

#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

	for(i=0; i<(width*height*3); i++)
			MINMAX( rgb[i], min, max  );

	amplify = 255.0/(max-min);

	for(i=0; i<(width*height*3); i++)
	{
		int val = amplify * (rgb[i] - min);
		
		if(val < brightness_adjust)
		   rgb[i] = val * 2;
		else if (val > (255 - brightness_adjust))
		   rgb[i] = 255;
		else
		   rgb[i] = val + brightness_adjust;
	}
	
	return GP_OK;
}