/
texgz-plugin.c
440 lines (388 loc) · 12.2 KB
/
texgz-plugin.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright (c) 2011 Jeff Boody
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "texgz_tex.c"
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
static gint32 texgz_import(const gchar *filename)
{
assert(filename);
LOGD("debug filename=%s", filename);
texgz_tex_t* tex = texgz_tex_import(filename);
if(tex == NULL)
return -1;
if(texgz_tex_convert(tex, TEXGZ_UNSIGNED_BYTE, TEXGZ_RGBA) == 0)
goto fail_convert;
gint32 image_ID = gimp_image_new(tex->stride, tex->vstride, GIMP_RGB);
if(image_ID == -1)
{
LOGE("gimp_image_new failed");
goto fail_gimp_image_new;
}
if(gimp_image_set_filename(image_ID, filename) == FALSE)
{
LOGE("gimp_image_set_filename failed");
goto fail_gimp_image_set_filename;
}
gint32 layer_ID = gimp_layer_new(image_ID, "texgz", tex->stride, tex->vstride,
GIMP_RGBA_IMAGE, 100, GIMP_NORMAL_MODE);
if(layer_ID == -1)
{
LOGE("gimp_layer_new failed");
goto fail_gimp_layer_new;
}
if(gimp_image_add_layer(image_ID, layer_ID, -1) == FALSE)
{
LOGE("gimp_image_add_layer failed");
goto fail_gimp_image_add_layer;
}
if(gimp_layer_set_offsets(layer_ID, 0, 0) == FALSE)
{
LOGE("gimp_layer_set_offsets failed");
goto fail_gimp_layer_set_offsets;
}
if(gimp_layer_set_lock_alpha(layer_ID, FALSE) == FALSE)
{
LOGE("gimp_layer_set_lock_alpha failed");
goto fail_gimp_layer_set_lock_alpha;
}
GimpDrawable* drawable = gimp_drawable_get(layer_ID);
if(drawable == NULL)
{
LOGE("gimp_drawable_get failed");
goto fail_gimp_drawable_get;
}
GimpPixelRgn pixel_rgn;
gimp_pixel_rgn_init(&pixel_rgn, drawable,
0, 0, drawable->width, drawable->height,
FALSE, FALSE);
gimp_pixel_rgn_set_rect(&pixel_rgn, tex->pixels,
0, 0, tex->stride, tex->vstride);
gimp_drawable_flush(drawable);
texgz_tex_delete(&tex);
// success
return image_ID;
// failure
fail_gimp_drawable_get:
fail_gimp_layer_set_lock_alpha:
fail_gimp_layer_set_offsets:
fail_gimp_image_add_layer:
// gimp_layer_delete does not exist
fail_gimp_layer_new:
fail_gimp_image_set_filename:
gimp_image_delete(image_ID);
fail_gimp_image_new:
fail_convert:
texgz_tex_delete(&tex);
return -1;
}
typedef struct
{
gint param;
} texgz_dialog_t;
static gint texgz_export_dialog(texgz_dialog_t* self)
{
assert(self);
LOGD("debug");
GtkWidget* dialog = gimp_dialog_new("Save as texgz",
"texgz-export",
NULL, 0,
NULL,
NULL,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE,
GTK_RESPONSE_OK,
NULL);
if(dialog == NULL)
{
LOGE("gimp_dialog_new failed");
return 0;
}
gimp_window_set_transient(GTK_WINDOW(dialog));
GtkWidget* frame = gimp_int_radio_group_new(TRUE,
"Format",
G_CALLBACK(gimp_radio_button_update),
&self->param,
8888,
"RGBA-8888", 8888, NULL,
"BGRA-8888", 8889, NULL,
"RGB-565", 565, NULL,
"RGBA-4444", 4444, NULL,
"RGB-888", 888, NULL,
"RGBA-5551", 5551, NULL,
"LUMINANCE", 8, NULL,
"LUMINANCE-F", 0xF, NULL,
NULL);
if(frame == NULL)
{
LOGE("gimp_int_radio_group_new failed");
goto fail_gimp_int_radio_group_new;
}
gtk_container_set_border_width(GTK_CONTAINER(frame), 6);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, TRUE, 0);
gtk_widget_show(frame);
gtk_widget_show(dialog);
if(gimp_dialog_run(GIMP_DIALOG(dialog)) != GTK_RESPONSE_OK)
{
LOGE("gimp_dialog_run failed");
goto fail_gimp_dialog_run;
}
gtk_widget_destroy(dialog);
// success
return 1;
// failure
fail_gimp_dialog_run:
fail_gimp_int_radio_group_new:
gtk_widget_destroy(dialog); // Does this delete the frame also?
return 0;
}
static int texgz_export(const gchar* filename,
gint32 image_ID,
gint32 drawable_ID,
texgz_dialog_t* dialog)
{
assert(filename);
LOGD("debug filename=%s", filename);
GimpImageType drawable_type = gimp_drawable_type(drawable_ID);
if(drawable_type != GIMP_RGBA_IMAGE)
{
LOGE("invalid drawable type");
return 0;
}
int type;
int format;
if(dialog->param == 8888)
{
type = TEXGZ_UNSIGNED_BYTE;
format = TEXGZ_RGBA;
}
else if(dialog->param == 8889)
{
type = TEXGZ_UNSIGNED_BYTE;
format = TEXGZ_BGRA;
}
else if(dialog->param == 565)
{
type = TEXGZ_UNSIGNED_SHORT_5_6_5;
format = TEXGZ_RGB;
}
else if(dialog->param == 4444)
{
type = TEXGZ_UNSIGNED_SHORT_4_4_4_4;
format = TEXGZ_RGBA;
}
else if(dialog->param == 888)
{
type = TEXGZ_UNSIGNED_BYTE;
format = TEXGZ_RGB;
}
else if(dialog->param == 5551)
{
type = TEXGZ_UNSIGNED_SHORT_5_5_5_1;
format = TEXGZ_RGBA;
}
else if(dialog->param == 8)
{
type = TEXGZ_UNSIGNED_BYTE;
format = TEXGZ_LUMINANCE;
}
else if(dialog->param == 0xF)
{
type = TEXGZ_FLOAT;
format = TEXGZ_LUMINANCE;
}
else
{
LOGE("invalid param=%i", dialog->param);
return 0;
}
GimpDrawable* drawable = gimp_drawable_get(drawable_ID);
if(drawable == NULL)
{
LOGE("could not get drawable");
return 0;
}
GimpPixelRgn pixel_rgn;
gimp_pixel_rgn_init(&pixel_rgn, drawable,
0, 0, drawable->width, drawable->height,
FALSE, FALSE);
texgz_tex_t* tex = texgz_tex_new(drawable->width, drawable->height,
drawable->width, drawable->height,
TEXGZ_UNSIGNED_BYTE, TEXGZ_RGBA,
NULL);
if(tex == NULL)
goto fail_texgz_tex_new;
gimp_pixel_rgn_get_rect(&pixel_rgn, tex->pixels,
0, 0, tex->stride, tex->vstride);
if(texgz_tex_convert(tex, type, format) == 0)
goto fail_texgz_tex_convert;
if(texgz_tex_export(tex, filename) == 0)
goto fail_texgz_tex_export;
texgz_tex_delete(&tex);
gimp_drawable_detach(drawable);
// success
return 1;
// failure
fail_texgz_tex_export:
fail_texgz_tex_convert:
texgz_tex_delete(&tex);
fail_texgz_tex_new:
gimp_drawable_detach(drawable);
return 0;
}
static void query(void)
{
static const GimpParamDef s_params_load[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to load" }
};
static const GimpParamDef s_params_save[] =
{
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
{ GIMP_PDB_STRING, "raw-filename", "The name of the file to save the image in" },
{ GIMP_PDB_INT32, "raw", "Specify non-zero for raw output, zero for ascii output" }
};
static GimpParamDef s_return_vals[] =
{
{ GIMP_PDB_IMAGE, "image", "Output image" }
};
LOGD("debug");
gimp_install_procedure("texgz-import",
"https://github.com/jeffboody/texgz",
"Loads texgz images",
"Jeff Boody",
"Copyright (c) 2011 Jeff Boody",
"2011",
"texgz image",
NULL,
GIMP_PLUGIN,
G_N_ELEMENTS(s_params_load),
G_N_ELEMENTS(s_return_vals),
s_params_load,
s_return_vals);
gimp_install_procedure("texgz-export",
"https://github.com/jeffboody/texgz",
"Saves texgz images",
"Jeff Boody",
"Copyright (c) 2011 Jeff Boody",
"2011",
"texgz image",
"RGBA",
GIMP_PLUGIN,
G_N_ELEMENTS(s_params_save),
0,
s_params_save,
NULL);
gimp_register_file_handler_mime("texgz-import", "image/texgz");
gimp_register_file_handler_mime("texgz-export", "image/texgz");
gimp_register_load_handler("texgz-import", "texgz", "");
gimp_register_save_handler("texgz-export", "texgz", "");
}
static void run(const gchar *name,
gint nparams,
const GimpParam* param,
gint* nreturn_vals,
GimpParam** return_vals)
{
static GimpParam s_return_vals[2];
gint32 image_ID;
gint32 drawable_ID;
GimpRunMode run_mode = param[0].data.d_int32;
assert(name);
assert(param);
assert(nreturn_vals);
assert(return_vals);
LOGD("debug name=%s", name);
*nreturn_vals = 1;
*return_vals = s_return_vals;
s_return_vals[0].type = GIMP_PDB_STATUS;
s_return_vals[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
if(strcmp(name, "texgz-import") == 0)
{
image_ID = texgz_import(param[1].data.d_string);
if(image_ID == -1)
{
LOGE("texgz_import failed");
return;
}
*nreturn_vals = 2;
s_return_vals[0].data.d_status = GIMP_PDB_SUCCESS;
s_return_vals[1].type = GIMP_PDB_IMAGE;
s_return_vals[1].data.d_image = image_ID;
}
else if(strcmp(name, "texgz-export") == 0)
{
image_ID = param[1].data.d_int32;
drawable_ID = param[2].data.d_int32;
if(run_mode == GIMP_RUN_INTERACTIVE)
{
gimp_ui_init("texgz-export", FALSE);
if(gimp_export_image(&image_ID,
&drawable_ID,
"texgz",
GIMP_EXPORT_CAN_HANDLE_RGB |
GIMP_EXPORT_NEEDS_ALPHA
) == GIMP_EXPORT_CANCEL)
{
LOGE("gimp_export_image failed");
s_return_vals[0].data.d_status = GIMP_PDB_CANCEL;
return;
}
texgz_dialog_t dialog = { 8888 };
if(texgz_export_dialog(&dialog) == 0)
{
LOGE("texgz_export_dialog failed");
s_return_vals[0].data.d_status = GIMP_PDB_CANCEL;
gimp_image_delete(image_ID);
return;
}
if(texgz_export(param[3].data.d_string, image_ID, drawable_ID, &dialog) == 0)
{
LOGE("texgz_export failed");
s_return_vals[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
gimp_image_delete(image_ID);
return;
}
s_return_vals[0].data.d_status = GIMP_PDB_SUCCESS;
}
}
else
{
LOGE("invalid name=%s", name);
s_return_vals[0].data.d_status = GIMP_PDB_CALLING_ERROR;
return;
}
}
GimpPlugInInfo PLUG_IN_INFO =
{
NULL,
NULL,
query,
run
};
MAIN()