summaryrefslogtreecommitdiff
path: root/camlibs/polaroid/dlink350f.c
blob: 8d4cf88801291e1b7f005c75755610c4ae1c6b80 (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
/* dlink350f.c
 *
 * orientation, byte order, and brightness correction for the D-Link 350F
 *
 * Copyright 2003 Mark Slemko <slemkom@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., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "config.h"

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

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

#define GP_MODULE "dlink350f"

#include "dlink350f.h"

#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

/*
 * function added for the D-Link DSC 350F - written by Mark Slemko - mslemko@netscape.net
 * This function correctly adjusts the color and orientation of the image
 */
int dlink_dsc350f_postprocessing_and_flip_both (int width, int height, unsigned char* rgb) {
	unsigned char *start, *end, c;

	int whichcolor = 0;
	int lowred=255, lowgreen=255, lowblue=255;
	int hired=0, higreen=0, hiblue=0;
	
	GP_DEBUG("flipping byte order");
	
	/* flip image left/right and top/bottom (actually reverse byte order) */
	start = rgb;
	end = start + ((width * height) * 3);
	
	while (start < end) {
		c = *start;
		
		/* validation - debugging info - collect the color range info 
		 * for first half of image.
		 */
		switch (whichcolor % 3) {
			case 0: /* blue */
				MINMAX((int)c,lowblue,hiblue);
				break;
			case 1: /* green */
				MINMAX((int)c,lowgreen,higreen);
				break;
			default: /* red */
				MINMAX((int)c,lowred,hired);
				break;
		}
		
		/* adjust color magnitude, since it appears that the 350f only had 7 bits of color info */
		*start++ = *--end << 1;
		*end = c << 1;
		
		whichcolor++;
	}
	
	/* could do more color processing here
	GP_DEBUG("adjusting color");
	
	// adjust image colours
	start = rgb;
	end = start + ((width * height) * 3);
	
	while (start < end) {
		c = *start++;
	}
	*/
	
	/* show the color range of image in debug mode. */
	GP_DEBUG("\nred low = %d high = %d\ngreen low = %d high = %d\nblue low = %d high = %d\n", lowred,hired, lowgreen,higreen, lowblue,hiblue);
	return GP_OK;
}