1 // ==========================================================
2 // Kodak PhotoCD Loader
3 //
4 // Design and implementation by
5 // - Floris van den Berg (flvdberg@wxs.nl)
6 //
7 // Based on pascal code developed by Alex Kwak
8 //
9 // This file is part of FreeImage 3
10 //
11 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
12 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
13 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
14 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
15 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
16 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
17 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
18 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
19 // THIS DISCLAIMER.
20 //
21 // Use at your own risk!
22 // ==========================================================
23
24 #include "FreeImage.h"
25 #include "Utilities.h"
26
27 // ==========================================================
28 // Internal functions
29 // ==========================================================
30
31 static int
clamp(double x)32 clamp(double x) {
33 int a = (int)floor(x + 0.5);
34 return (a < 0) ? 0 : (a > 255) ? 255 : a;
35 }
36
37 static void
YUV2RGB(int y,int cb,int cr,int & r,int & g,int & b)38 YUV2RGB(int y, int cb, int cr, int &r, int &g, int &b) {
39 double c11 = 0.0054980 * 256.0;
40 double c12 = 0.0000001 * 256.0;
41 double c13 = 0.0051681 * 256.0;
42 double c21 = 0.0054980 * 256.0;
43 double c22 = -0.0015446 * 256.0;
44 double c23 = -0.0026325 * 256.0;
45 double c31 = 0.0054980 * 256.0;
46 double c32 = 0.0079533 * 256.0;
47 double c33 = 0.0000001 * 256.0;
48
49 r = clamp(c11 * y + c12 * (cb - 156) + c13 * (cr - 137));
50 g = clamp(c21 * y + c22 * (cb - 156) + c23 * (cr - 137));
51 b = clamp(c31 * y + c32 * (cb - 156) + c33 * (cr - 137));
52 }
53
54 static BOOL
VerticalOrientation(FreeImageIO * io,fi_handle handle)55 VerticalOrientation(FreeImageIO *io, fi_handle handle) {
56 char buffer[128];
57
58 io->read_proc(buffer, 128, 1, handle);
59
60 return (buffer[72] & 63) == 8;
61 }
62
63 // ==========================================================
64 // Plugin Interface
65 // ==========================================================
66
67 static int s_format_id;
68
69 // ==========================================================
70 // Plugin Implementation
71 // ==========================================================
72
73 static const char * DLL_CALLCONV
Format()74 Format() {
75 return "PCD";
76 }
77
78 static const char * DLL_CALLCONV
Description()79 Description() {
80 return "Kodak PhotoCD";
81 }
82
83 static const char * DLL_CALLCONV
Extension()84 Extension() {
85 return "pcd";
86 }
87
88 static const char * DLL_CALLCONV
RegExpr()89 RegExpr() {
90 return NULL;
91 }
92
93 static const char * DLL_CALLCONV
MimeType()94 MimeType() {
95 return "image/x-photo-cd";
96 }
97
98 static BOOL DLL_CALLCONV
SupportsExportDepth(int depth)99 SupportsExportDepth(int depth) {
100 return FALSE;
101 }
102
103 static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type)104 SupportsExportType(FREE_IMAGE_TYPE type) {
105 return FALSE;
106 }
107
108 static BOOL DLL_CALLCONV
SupportsNoPixels()109 SupportsNoPixels() {
110 return TRUE;
111 }
112
113 // ----------------------------------------------------------
114
115 static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO * io,fi_handle handle,int page,int flags,void * data)116 Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
117 FIBITMAP *dib = NULL;
118 unsigned width;
119 unsigned height;
120 const unsigned bpp = 24;
121 int scan_line_add = 1;
122 int start_scan_line = 0;
123
124 BYTE *y1 = NULL, *y2 = NULL, *cbcr = NULL;
125
126 BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
127
128 // to make absolute seeks possible we store the current position in the file
129
130 long offset_in_file = io->tell_proc(handle);
131 long seek = 0;
132
133 // decide which bitmap in the cabinet to load
134
135 switch (flags) {
136 case PCD_BASEDIV4 :
137 seek = 0x2000;
138 width = 192;
139 height = 128;
140 break;
141
142 case PCD_BASEDIV16 :
143 seek = 0xB800;
144 width = 384;
145 height = 256;
146 break;
147
148 default :
149 seek = 0x30000;
150 width = 768;
151 height = 512;
152 break;
153 }
154
155 try {
156 // allocate the dib and write out the header
157 dib = FreeImage_AllocateHeader(header_only, width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
158 if(!dib) throw FI_MSG_ERROR_DIB_MEMORY;
159
160 if(header_only) {
161 return dib;
162 }
163
164 // check if the PCD is bottom-up
165
166 if (VerticalOrientation(io, handle)) {
167 scan_line_add = -1;
168 start_scan_line = height - 1;
169 }
170
171 // temporary stuff to load PCD
172
173 BYTE *y1 = (BYTE*)malloc(width * sizeof(BYTE));
174 BYTE *y2 = (BYTE*)malloc(width * sizeof(BYTE));
175 BYTE *cbcr = (BYTE*)malloc(width * sizeof(BYTE));
176 if(!y1 || !y2 || !cbcr) throw FI_MSG_ERROR_MEMORY;
177
178 BYTE *yl[] = { y1, y2 };
179
180 // seek to the part where the bitmap data begins
181
182 io->seek_proc(handle, offset_in_file, SEEK_SET);
183 io->seek_proc(handle, seek, SEEK_CUR);
184
185 // read the data
186
187 for (unsigned y = 0; y < height / 2; y++) {
188 io->read_proc(y1, width, 1, handle);
189 io->read_proc(y2, width, 1, handle);
190 io->read_proc(cbcr, width, 1, handle);
191
192 for (int i = 0; i < 2; i++) {
193 BYTE *bits = FreeImage_GetScanLine(dib, start_scan_line);
194 for (unsigned x = 0; x < width; x++) {
195 int r, g, b;
196
197 YUV2RGB(yl[i][x], cbcr[x / 2], cbcr[(width / 2) + (x / 2)], r, g, b);
198
199 bits[FI_RGBA_BLUE] = (BYTE)b;
200 bits[FI_RGBA_GREEN] = (BYTE)g;
201 bits[FI_RGBA_RED] = (BYTE)r;
202 bits += 3;
203 }
204
205 start_scan_line += scan_line_add;
206 }
207 }
208
209 free(cbcr);
210 free(y2);
211 free(y1);
212
213 return dib;
214
215 } catch(const char *text) {
216 if(dib) FreeImage_Unload(dib);
217 if(cbcr) free(cbcr);
218 if(y2) free(y2);
219 if(y1) free(y1);
220
221 FreeImage_OutputMessageProc(s_format_id, text);
222
223 return NULL;
224 }
225 }
226
227 // ==========================================================
228 // Init
229 // ==========================================================
230
231 void DLL_CALLCONV
InitPCD(Plugin * plugin,int format_id)232 InitPCD(Plugin *plugin, int format_id) {
233 s_format_id = format_id;
234
235 plugin->format_proc = Format;
236 plugin->description_proc = Description;
237 plugin->extension_proc = Extension;
238 plugin->regexpr_proc = RegExpr;
239 plugin->open_proc = NULL;
240 plugin->close_proc = NULL;
241 plugin->pagecount_proc = NULL;
242 plugin->pagecapability_proc = NULL;
243 plugin->load_proc = Load;
244 plugin->save_proc = NULL;
245 plugin->validate_proc = NULL;
246 plugin->mime_proc = MimeType;
247 plugin->supports_export_bpp_proc = SupportsExportDepth;
248 plugin->supports_export_type_proc = SupportsExportType;
249 plugin->supports_icc_profiles_proc = NULL;
250 plugin->supports_no_pixels_proc = SupportsNoPixels;
251 }
252