forked from marcelteun/GIMP-raw-file-load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-raw-load.c
357 lines (322 loc) · 8.59 KB
/
file-raw-load.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright (C) 2011 Marcel Tunnissen
*
* This file implements a GTK dialog for loading raw images used by
* GIMP plugins that load raw images in a RGB & YUV format.
*
* License: GNU Public License version 2
*
* This program 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.
*
* This program 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 this program; if not,
* check at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or write to the Free Software Foundation,
*/
#include <stdio.h>
#include <libgimp/gimp.h>
#include "file-raw-load.h"
#include "file-raw-load-gtk.h"
#include "yuv-convert.h"
#define IDX_RED 0
#define IDX_GREEN 1
#define IDX_BLUE 2
static gint32 create_new_layer(
gint32 img,
gint position,
const gchar* layername,
guint width,
guint height,
GimpImageBaseType bas_type,
GimpDrawable** drawable,
GimpPixelRgn* pixel_rgn)
{
gint32 layer;
GimpImageType img_type = GIMP_RGB_IMAGE;
/* TODO: add support for GIMP_RGBA_IMAGE in case of RGBA888 */
switch (bas_type)
{
case GIMP_RGB:
img_type = GIMP_RGB_IMAGE;
break;
case GIMP_GRAY:
img_type = GIMP_GRAY_IMAGE;
break;
case GIMP_INDEXED:
img_type = GIMP_INDEXED_IMAGE;
break;
}
if (!layername) {
layername = "background";
}
layer = gimp_layer_new(img, layername, width, height, img_type, 100,
GIMP_NORMAL_MODE);
gimp_image_add_layer(img, layer, position);
*drawable = gimp_drawable_get(layer);
gimp_pixel_rgn_init(pixel_rgn, *drawable, 0, 0, (*drawable)->width,
(*drawable)->height, TRUE, FALSE);
return layer;
}
static gint32 create_new_image(
const gchar* filename,
const gchar* layername,
guint width,
guint height,
GimpImageBaseType bas_type,
//gdouble xres,
//gdouble yres,
gint32* layer,
GimpDrawable** drawable,
GimpPixelRgn* pixel_rgn
) {
gint32 img;
img = gimp_image_new(width, height, bas_type);
gimp_image_undo_disable(img);
gimp_image_set_filename(img, filename);
//gimp_image_set_resolution(img, xres, yres);
*layer = create_new_layer(img, 0, layername, width, height, bas_type,
drawable, pixel_rgn);
return img;
}
static guchar read_yuv_pixel(FILE* file, enum pix_fmt fmt, guchar yuv[3]) {
/*
* returns ERROR if an EOF occurred or if the fmt is not supported
*/
static guchar yuv_sav[3];
static int odd_pixel = FALSE;
switch (fmt) {
case UYVY_422:
if (odd_pixel) {
yuv[0] = yuv_sav[0];
yuv[1] = yuv_sav[1];
yuv[2] = yuv_sav[2];
} else {
if ((yuv[1] = fgetc(file)) == EOF) return ERROR;
if ((yuv[0] = fgetc(file)) == EOF) return ERROR;
if ((yuv[2] = fgetc(file)) == EOF) return ERROR;
if ((yuv_sav[0] = fgetc(file)) == EOF) return ERROR;
yuv_sav[1] = yuv[1];
yuv_sav[2] = yuv[2];
}
odd_pixel = !odd_pixel;
return OK;
break;
case YUYV_422:
if (odd_pixel) {
yuv[0] = yuv_sav[0];
yuv[1] = yuv_sav[1];
yuv[2] = yuv_sav[2];
} else {
if ((yuv[0] = fgetc(file)) == EOF) return ERROR;
if ((yuv[1] = fgetc(file)) == EOF) return ERROR;
if ((yuv_sav[0] = fgetc(file)) == EOF) return ERROR;
if ((yuv[2] = fgetc(file)) == EOF) return ERROR;
yuv_sav[1] = yuv[1];
yuv_sav[2] = yuv[2];
}
odd_pixel = !odd_pixel;
return OK;
break;
default:
return ERROR;
break;
}
}
static guchar read_rgb_pixel(FILE* file, enum pix_fmt fmt, guchar rgb[3]) {
/*
* returns ERROR if an EOF occurred or if the fmt is not supported
*/
guchar c;
switch (fmt) {
case RGB_565:
/* read (least significant) part of GREEN and BLUE */
if ((c = fgetc(file)) == EOF) return ERROR;
rgb[IDX_GREEN] = (c & 0xe0) >> 3;
rgb[IDX_BLUE] = (c & 0x1f) << 3;
/* read RED and (most significant) part of GREEN */
if ((c = fgetc(file)) == EOF) return ERROR;
rgb[IDX_RED] = c & 0xf8;
rgb[IDX_GREEN] = rgb[1] | (c & 0x07) << 5;
return OK;
break;
case RGB_888:
if ((rgb[0] = fgetc(file)) == EOF) return ERROR;
if ((rgb[1] = fgetc(file)) == EOF) return ERROR;
if ((rgb[2] = fgetc(file)) == EOF) return ERROR;
return OK;
break;
case UYVY_422:
/* intentional fall through */
case YUYV_422:
{
guchar yuv[3];
if (read_yuv_pixel(file, fmt, yuv) != ERROR) {
convert_yuv2rgb(yuv, rgb);
return OK;
} else {
return ERROR;
}
}
break;
case BGR_888:
if ((rgb[2] = fgetc(file)) == EOF) return ERROR;
if ((rgb[1] = fgetc(file)) == EOF) return ERROR;
if ((rgb[0] = fgetc(file)) == EOF) return ERROR;
return OK;
break;
default:
return ERROR;
break;
}
}
static gint32 load_raw(
char* filename,
FILE* file,
enum pix_fmt fmt,
struct raw_data* img_data,
const gchar* plugin_name
) {
gint nr_chls;
gint x, y;
gint32 img;
gint32 layer;
GimpDrawable* drawable;
GimpPixelRgn pixel_rgn;
guchar *pix_row;
guint width = img_data->size[0];
guint height = img_data->size[1];
if (width <= 0 || height <= 0) {
return ERROR;
}
img = create_new_image(filename, NULL, width, height, GIMP_RGB, &layer,
&drawable, &pixel_rgn);
nr_chls = gimp_drawable_bpp(drawable->drawable_id);
pix_row = g_new(guchar, nr_chls * width);
x = 0;
y = 0;
while (1) {
guchar rgb[3];
if (read_rgb_pixel(file, fmt, rgb) == ERROR)
return ERROR;
pix_row[nr_chls * x] = rgb[IDX_RED];
pix_row[nr_chls * x + 1] = rgb[IDX_GREEN];
pix_row[nr_chls * x + 2] = rgb[IDX_BLUE];
x++;
if (x == width) {
gimp_pixel_rgn_set_row(&pixel_rgn, pix_row, 0, y, width);
x = 0;
y++;
}
if (y == height) break;
}
if (fgetc(file) != EOF) {
printf("%s: warning: file contains more data\n", plugin_name);
}
return img;
}
static gint32 open_raw(
char* filename,
enum pix_fmt fmt,
struct raw_data* img_data,
const gchar* plugin_name
) {
gint32 img;
FILE *file;
file = fopen(filename, "rb");
if (!file)
{
show_message("can't open file for reading");
return(ERROR);
}
img = load_raw(filename, file, fmt, img_data, plugin_name);
fclose (file);
return img;
}
/*
* EXTERNALS
*/
void run(
const gchar* plugin_name,
gint nparams,
const GimpParam* param,
gint* nreturn_vals,
GimpParam** return_vals,
enum pix_fmt fmt,
struct raw_data* img_data
) {
static GimpParam values[2];
GimpRunMode run_mode;
gchar* check_button_label;
gint32 img = ERROR;
if (!img_data) goto call_error;
run_mode = param[0].data.d_int32;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_SUCCESS;
values[1].type = GIMP_PDB_IMAGE;
values[1].data.d_image = ERROR;
*return_vals = values;
*nreturn_vals = 2;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data(plugin_name, img_data);
switch(fmt) {
case RGB_888:
check_button_label = "BGR";
break;
case UYVY_422:
check_button_label = "YUYV";
break;
default:
check_button_label = NULL;
break;
}
if (!load_dialog(img_data, check_button_label))
goto exec_error;
break;
case GIMP_RUN_NONINTERACTIVE:
/* TODO */
goto call_error;
break;
case GIMP_RUN_WITH_LAST_VALS:
gimp_get_data(plugin_name, img_data);
break;
}
if (img_data->checked) {
switch(fmt) {
case RGB_888:
fmt = BGR_888;
break;
case UYVY_422:
fmt = YUYV_422;
break;
default:
break;
}
}
img = open_raw(param[1].data.d_string, fmt, img_data, plugin_name);
if (img == ERROR)
goto exec_error;
if (run_mode != GIMP_RUN_INTERACTIVE) {
gimp_image_clean_all(img);
} else {
gimp_set_data(plugin_name, img_data, sizeof(struct raw_data));
}
values[0].data.d_status = GIMP_PDB_SUCCESS;
values[1].data.d_image = img;
return;
call_error:
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
return;
exec_error:
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
return;
}