2
* "$Id: sgi.c,v 1.47 2004/10/28 12:12:52 neo Exp $"
4
* SGI image file plug-in for the GIMP.
6
* Copyright 1997-1998 Michael Sweet (mike@easysw.com)
8
* This program is free software; you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License as published by the Free
10
* Software Foundation; either version 2 of the License, or (at your option)
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
* main() - Main entry - just call gimp_main()...
25
* query() - Respond to a plug-in query...
26
* run() - Run the plug-in...
27
* load_image() - Load a PNG image into a new image window.
28
* save_image() - Save the specified image to a PNG file.
29
* save_ok_callback() - Destroy the save dialog and save the image.
30
* save_dialog() - Pop up the save dialog.
42
#include <libgimp/gimp.h>
43
#include <libgimp/gimpui.h>
45
#include "sgi.h" /* SGI image library definitions */
47
#include "libgimp/stdplugins-intl.h"
54
#define PLUG_IN_VERSION "1.1.1 - 17 May 1998"
61
static void query (void);
62
static void run (const gchar *name,
64
const GimpParam *param,
66
GimpParam **return_vals);
68
static gint32 load_image (const gchar *filename);
69
static gint save_image (const gchar *filename,
73
static gboolean save_dialog (void);
79
GimpPlugInInfo PLUG_IN_INFO =
83
query, /* query_proc */
87
static gint compression = SGI_COMP_RLE;
95
static GimpParamDef load_args[] =
97
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
98
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
99
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to load" },
101
static GimpParamDef load_return_vals[] =
103
{ GIMP_PDB_IMAGE, "image", "Output image" },
106
static GimpParamDef save_args[] =
108
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
109
{ GIMP_PDB_IMAGE, "image", "Input image" },
110
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
111
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
112
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to save the image in" },
113
{ GIMP_PDB_INT32, "compression", "Compression level (0 = none, 1 = RLE, 2 = ARLE)" }
116
gimp_install_procedure ("file_sgi_load",
117
"Loads files in SGI image file format",
118
"This plug-in loads SGI image files.",
119
"Michael Sweet <mike@easysw.com>",
120
"Copyright 1997-1998 by Michael Sweet",
122
N_("Silicon Graphics IRIS image"),
125
G_N_ELEMENTS (load_args),
126
G_N_ELEMENTS (load_return_vals),
130
gimp_register_file_handler_mime ("file_sgi_load", "image/x-sgi");
131
gimp_register_magic_load_handler ("file_sgi_load",
136
gimp_install_procedure ("file_sgi_save",
137
"Saves files in SGI image file format",
138
"This plug-in saves SGI image files.",
139
"Michael Sweet <mike@easysw.com>",
140
"Copyright 1997-1998 by Michael Sweet",
142
N_("Silicon Graphics IRIS image"),
145
G_N_ELEMENTS (save_args),
150
gimp_register_file_handler_mime ("file_sgi_save", "image/x-sgi");
151
gimp_register_save_handler ("file_sgi_save", "sgi,rgb,bw,icon", "");
155
run (const gchar *name,
157
const GimpParam *param,
159
GimpParam **return_vals)
161
static GimpParam values[2];
162
GimpRunMode run_mode;
163
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
166
GimpExportReturn export = GIMP_EXPORT_CANCEL;
168
run_mode = param[0].data.d_int32;
171
*return_vals = values;
172
values[0].type = GIMP_PDB_STATUS;
173
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
177
if (strcmp (name, "file_sgi_load") == 0)
179
image_ID = load_image (param[1].data.d_string);
184
values[1].type = GIMP_PDB_IMAGE;
185
values[1].data.d_image = image_ID;
189
status = GIMP_PDB_EXECUTION_ERROR;
192
else if (strcmp (name, "file_sgi_save") == 0)
194
image_ID = param[1].data.d_int32;
195
drawable_ID = param[2].data.d_int32;
197
/* eventually export the image */
200
case GIMP_RUN_INTERACTIVE:
201
case GIMP_RUN_WITH_LAST_VALS:
202
gimp_ui_init ("sgi", FALSE);
203
export = gimp_export_image (&image_ID, &drawable_ID, "SGI",
204
(GIMP_EXPORT_CAN_HANDLE_RGB |
205
GIMP_EXPORT_CAN_HANDLE_GRAY |
206
GIMP_EXPORT_CAN_HANDLE_ALPHA));
207
if (export == GIMP_EXPORT_CANCEL)
209
values[0].data.d_status = GIMP_PDB_CANCEL;
219
case GIMP_RUN_INTERACTIVE:
221
* Possibly retrieve data...
223
gimp_get_data ("file_sgi_save", &compression);
226
* Then acquire information with a dialog...
229
status = GIMP_PDB_CANCEL;
232
case GIMP_RUN_NONINTERACTIVE:
234
* Make sure all the arguments are there!
238
status = GIMP_PDB_CALLING_ERROR;
242
compression = param[5].data.d_int32;
244
if (compression < 0 || compression > 2)
245
status = GIMP_PDB_CALLING_ERROR;
249
case GIMP_RUN_WITH_LAST_VALS:
251
* Possibly retrieve data...
253
gimp_get_data ("file_sgi_save", &compression);
260
if (status == GIMP_PDB_SUCCESS)
262
if (save_image (param[3].data.d_string, image_ID, drawable_ID))
264
gimp_set_data ("file_sgi_save",
265
&compression, sizeof (compression));
269
status = GIMP_PDB_EXECUTION_ERROR;
273
if (export == GIMP_EXPORT_EXPORT)
274
gimp_image_delete (image_ID);
278
status = GIMP_PDB_CALLING_ERROR;
281
values[0].data.d_status = status;
286
* 'load_image()' - Load a PNG image into a new image window.
290
load_image (const gchar *filename) /* I - File to load */
292
int i, /* Looping var */
293
x, /* Current X coordinate */
294
y, /* Current Y coordinate */
295
image_type, /* Type of image */
296
layer_type, /* Type of drawable/layer */
297
tile_height, /* Height of tile in GIMP */
298
count, /* Count of rows to put in image */
299
bytes; /* Number of channels to use */
300
sgi_t *sgip; /* File pointer */
301
gint32 image, /* Image */
303
GimpDrawable *drawable; /* Drawable for layer */
304
GimpPixelRgn pixel_rgn; /* Pixel region for layer */
305
guchar **pixels, /* Pixel rows */
306
*pixel, /* Pixel data */
307
*pptr; /* Current pixel */
308
gushort **rows; /* SGI image data */
309
gchar *progress; /* Title for progress display... */
312
* Open the file for reading...
315
sgip = sgiOpen ((char *) filename, SGI_READ, 0, 0, 0, 0, 0);
318
g_message (_("Could not open '%s' for reading."),
319
gimp_filename_to_utf8 (filename));
323
progress = g_strdup_printf (_("Opening '%s'..."),
324
gimp_filename_to_utf8 (filename));
325
gimp_progress_init (progress);
329
* Get the image dimensions and create the image...
336
case 1 : /* Grayscale */
337
image_type = GIMP_GRAY;
338
layer_type = GIMP_GRAY_IMAGE;
341
case 2 : /* Grayscale + alpha */
342
image_type = GIMP_GRAY;
343
layer_type = GIMP_GRAYA_IMAGE;
347
image_type = GIMP_RGB;
348
layer_type = GIMP_RGB_IMAGE;
352
image_type = GIMP_RGB;
353
layer_type = GIMP_RGBA_IMAGE;
357
image_type = GIMP_RGB;
358
layer_type = GIMP_RGBA_IMAGE;
363
image = gimp_image_new (sgip->xsize, sgip->ysize, image_type);
366
g_message ("Could not allocate new image");
370
gimp_image_set_filename (image, filename);
373
* Create the "background" layer to hold the image...
376
layer = gimp_layer_new (image, _("Background"), sgip->xsize, sgip->ysize,
377
layer_type, 100, GIMP_NORMAL_MODE);
378
gimp_image_add_layer (image, layer, 0);
381
* Get the drawable and set the pixel region for our load...
384
drawable = gimp_drawable_get (layer);
386
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
387
drawable->height, TRUE, FALSE);
390
* Temporary buffers...
393
tile_height = gimp_tile_height ();
394
pixel = g_new (guchar, tile_height * sgip->xsize * bytes);
395
pixels = g_new (guchar *, tile_height);
397
for (i = 0; i < tile_height; i ++)
398
pixels[i] = pixel + sgip->xsize * bytes * i;
400
rows = g_new (unsigned short *, sgip->zsize);
401
rows[0] = g_new (unsigned short, sgip->xsize * sgip->zsize);
403
for (i = 1; i < sgip->zsize; i ++)
404
rows[i] = rows[0] + i * sgip->xsize;
410
for (y = 0, count = 0;
414
if (count >= tile_height)
416
gimp_pixel_rgn_set_rect (&pixel_rgn, pixel,
417
0, y - count, drawable->width, count);
420
gimp_progress_update ((double) y / (double) sgip->ysize);
423
for (i = 0; i < sgip->zsize; i ++)
424
if (sgiGetRow (sgip, rows[i], sgip->ysize - 1 - y, i) < 0)
425
printf("sgiGetRow(sgip, rows[i], %d, %d) failed!\n",
426
sgip->ysize - 1 - y, i);
431
* 8-bit (unsigned) pixels...
434
for (x = 0, pptr = pixels[count]; x < sgip->xsize; x ++)
435
for (i = 0; i < bytes; i ++, pptr ++)
441
* 16-bit (unsigned) pixels...
444
for (x = 0, pptr = pixels[count]; x < sgip->xsize; x ++)
445
for (i = 0; i < bytes; i ++, pptr ++)
446
*pptr = rows[i][x] >> 8;
451
* Do the last n rows (count always > 0)
454
gimp_pixel_rgn_set_rect (&pixel_rgn, pixel, 0,
455
y - count, drawable->width, count);
458
* Done with the file...
469
* Update the display...
472
gimp_drawable_flush (drawable);
473
gimp_drawable_detach (drawable);
480
* 'save_image()' - Save the specified image to a SGI file.
484
save_image (const gchar *filename,
488
gint i, j, /* Looping var */
489
x, /* Current X coordinate */
490
y, /* Current Y coordinate */
491
tile_height, /* Height of tile in GIMP */
492
count, /* Count of rows to put in image */
493
zsize; /* Number of channels in file */
494
sgi_t *sgip; /* File pointer */
495
GimpDrawable *drawable; /* Drawable for layer */
496
GimpPixelRgn pixel_rgn; /* Pixel region for layer */
497
guchar **pixels, /* Pixel rows */
498
*pixel, /* Pixel data */
499
*pptr; /* Current pixel */
500
gushort **rows; /* SGI image data */
501
gchar *progress; /* Title for progress display... */
504
* Get the drawable for the current image...
507
drawable = gimp_drawable_get (drawable_ID);
509
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
510
drawable->height, FALSE, FALSE);
513
switch (gimp_drawable_type (drawable_ID))
515
case GIMP_GRAY_IMAGE :
518
case GIMP_GRAYA_IMAGE :
521
case GIMP_RGB_IMAGE :
524
case GIMP_RGBA_IMAGE :
528
g_message (_("Cannot operate on indexed color images."));
533
* Open the file for writing...
536
sgip = sgiOpen ((char *) filename, SGI_WRITE, compression, 1,
537
drawable->width, drawable->height, zsize);
540
g_message (_("Could not open '%s' for writing."),
541
gimp_filename_to_utf8 (filename));
545
progress = g_strdup_printf (_("Saving '%s'..."),
546
gimp_filename_to_utf8 (filename));
547
gimp_progress_init (progress);
551
* Allocate memory for "tile_height" rows...
554
tile_height = gimp_tile_height ();
555
pixel = g_new (guchar, tile_height * drawable->width * zsize);
556
pixels = g_new (guchar *, tile_height);
558
for (i = 0; i < tile_height; i ++)
559
pixels[i]= pixel + drawable->width * zsize * i;
561
rows = g_new (gushort *, sgip->zsize);
562
rows[0] = g_new (gushort, sgip->xsize * sgip->zsize);
564
for (i = 1; i < sgip->zsize; i ++)
565
rows[i] = rows[0] + i * sgip->xsize;
571
for (y = 0; y < drawable->height; y += count)
574
* Grab more pixel data...
577
if ((y + tile_height) >= drawable->height)
578
count = drawable->height - y;
582
gimp_pixel_rgn_get_rect (&pixel_rgn, pixel, 0, y, drawable->width, count);
585
* Convert to shorts and write each color plane separately...
588
for (i = 0, pptr = pixels[0]; i < count; i ++)
590
for (x = 0; x < drawable->width; x ++)
591
for (j = 0; j < zsize; j ++, pptr ++)
594
for (j = 0; j < zsize; j ++)
595
sgiPutRow (sgip, rows[j], drawable->height - 1 - y - i, j);
598
gimp_progress_update ((double) y / (double) drawable->height);
602
* Done with the file...
622
dlg = gimp_dialog_new (_("Save as SGI"), "sgi",
624
gimp_standard_help_func, "file-sgi-save",
626
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
627
GTK_STOCK_OK, GTK_RESPONSE_OK,
631
frame = gimp_int_radio_group_new (TRUE, _("Compression type"),
632
G_CALLBACK (gimp_radio_button_update),
633
&compression, compression,
637
_("RLE compression"),
639
_("Aggressive RLE\n(not supported by SGI)"),
644
gtk_container_set_border_width (GTK_CONTAINER (frame), 12);
645
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, TRUE, TRUE, 0);
646
gtk_widget_show (frame);
648
gtk_widget_show (dlg);
650
run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
652
gtk_widget_destroy (dlg);