2
* "$Id: sgilib.c,v 1.8 2003/04/07 11:59:33 neo Exp $"
4
* SGI image file format library routines.
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
* sgiClose() - Close an SGI image file.
25
* sgiGetRow() - Get a row of image data from a file.
26
* sgiOpen() - Open an SGI image file for reading or writing.
27
* sgiOpenFile() - Open an SGI image file for reading or writing.
28
* sgiPutRow() - Put a row of image data to a file.
29
* getlong() - Get a 32-bit big-endian integer.
30
* getshort() - Get a 16-bit big-endian integer.
31
* putlong() - Put a 32-bit big-endian integer.
32
* putshort() - Put a 16-bit big-endian integer.
33
* read_rle8() - Read 8-bit RLE data.
34
* read_rle16() - Read 16-bit RLE data.
35
* write_rle8() - Write 8-bit RLE data.
36
* write_rle16() - Write 16-bit RLE data.
41
* Revision 1.8 2003/04/07 11:59:33 neo
42
* 2003-04-07 Sven Neumann <sven@gimp.org>
44
* * plug-ins/sgi/sgi.h
45
* * plug-ins/sgi/sgilib.c: applied a patch from marek@aki.cz that
46
* adds support for reading SGI files in little-endian format. Fixes
49
* Revision 1.7 1998/06/06 23:22:21 yosh
50
* * adding Lighting plugin
52
* * updated despeckle, png, sgi, and sharpen
56
* Revision 1.5 1998/04/23 17:40:49 mike
57
* Updated to support 16-bit <unsigned> image data.
59
* Revision 1.4 1998/02/05 17:10:58 mike
60
* Added sgiOpenFile() function for opening an existing file pointer.
62
* Revision 1.3 1997/07/02 16:40:16 mike
63
* sgiOpen() wasn't opening files with "rb" or "wb+". This caused problems
64
* on PCs running Windows/DOS...
66
* Revision 1.2 1997/06/18 00:55:28 mike
67
* Updated to hold length table when writing.
68
* Updated to hold current length when doing ARLE.
69
* Wasn't writing length table on close.
70
* Wasn't saving new line into arle_row when necessary.
72
* Revision 1.1 1997/06/15 03:37:19 mike
83
static int getlong(sgi_t*);
84
static int getshort(sgi_t*);
85
static int putlong(long, sgi_t*);
86
static int putshort(unsigned short, sgi_t*);
87
static int read_rle8(sgi_t*, unsigned short *, int);
88
static int read_rle16(sgi_t*, unsigned short *, int);
89
static int write_rle8(sgi_t*, unsigned short *, int);
90
static int write_rle16(sgi_t*, unsigned short *, int);
94
* 'sgiClose()' - Close an SGI image file.
98
sgiClose(sgi_t *sgip) /* I - SGI image */
100
int i; /* Return status */
101
long *offset; /* Looping var for offset table */
107
if (sgip->mode == SGI_WRITE && sgip->comp != SGI_COMP_NONE)
110
* Write the scanline offset table to the file...
113
fseek(sgip->file, 512, SEEK_SET);
115
for (i = sgip->ysize * sgip->zsize, offset = sgip->table[0];
118
if (putlong(offset[0], sgip) < 0)
121
for (i = sgip->ysize * sgip->zsize, offset = sgip->length[0];
124
if (putlong(offset[0], sgip) < 0)
128
if (sgip->table != NULL)
130
free(sgip->table[0]);
134
if (sgip->length != NULL)
136
free(sgip->length[0]);
140
if (sgip->comp == SGI_COMP_ARLE)
141
free(sgip->arle_row);
143
i = fclose(sgip->file);
151
* 'sgiGetRow()' - Get a row of image data from a file.
155
sgiGetRow(sgi_t *sgip, /* I - SGI image */
156
unsigned short *row, /* O - Row to read */
157
int y, /* I - Line to read */
158
int z) /* I - Channel to read */
160
int x; /* X coordinate */
161
long offset; /* File offset */
166
y < 0 || y >= sgip->ysize ||
167
z < 0 || z >= sgip->zsize)
174
* Seek to the image row - optimize buffering by only seeking if
178
offset = 512 + (y + z * sgip->ysize) * sgip->xsize * sgip->bpp;
179
if (offset != ftell(sgip->file))
180
fseek(sgip->file, offset, SEEK_SET);
184
for (x = sgip->xsize; x > 0; x --, row ++)
185
*row = getc(sgip->file);
189
for (x = sgip->xsize; x > 0; x --, row ++)
190
*row = getshort(sgip);
195
offset = sgip->table[z][y];
196
if (offset != ftell(sgip->file))
197
fseek(sgip->file, offset, SEEK_SET);
200
return (read_rle8(sgip, row, sgip->xsize));
202
return (read_rle16(sgip, row, sgip->xsize));
211
* 'sgiOpen()' - Open an SGI image file for reading or writing.
215
sgiOpen(char *filename, /* I - File to open */
216
int mode, /* I - Open mode (SGI_READ or SGI_WRITE) */
217
int comp, /* I - Type of compression */
218
int bpp, /* I - Bytes per pixel */
219
int xsize, /* I - Width of image in pixels */
220
int ysize, /* I - Height of image in pixels */
221
int zsize) /* I - Number of channels */
223
sgi_t *sgip; /* New SGI image file */
224
FILE *file; /* Image file pointer */
227
if (mode == SGI_READ)
228
file = fopen(filename, "rb");
230
file = fopen(filename, "wb+");
235
if ((sgip = sgiOpenFile(file, mode, comp, bpp, xsize, ysize, zsize)) == NULL)
243
* 'sgiOpenFile()' - Open an SGI image file for reading or writing.
247
sgiOpenFile(FILE *file, /* I - File to open */
248
int mode, /* I - Open mode (SGI_READ or SGI_WRITE) */
249
int comp, /* I - Type of compression */
250
int bpp, /* I - Bytes per pixel */
251
int xsize, /* I - Width of image in pixels */
252
int ysize, /* I - Height of image in pixels */
253
int zsize) /* I - Number of channels */
255
int i, j; /* Looping var */
256
char name[80]; /* Name of file in image header */
257
short magic; /* Magic number */
258
sgi_t *sgip; /* New image pointer */
261
if ((sgip = calloc(sizeof(sgi_t), 1)) == NULL)
270
sgip->mode = SGI_READ;
272
magic = getshort(sgip);
273
if (magic != SGI_MAGIC)
275
/* try little endian format */
276
magic = ((magic >> 8) & 0x00ff) | ((magic << 8) & 0xff00);
277
if(magic != SGI_MAGIC) {
285
sgip->comp = getc(sgip->file);
286
sgip->bpp = getc(sgip->file);
287
getshort(sgip); /* Dimensions */
288
sgip->xsize = getshort(sgip);
289
sgip->ysize = getshort(sgip);
290
sgip->zsize = getshort(sgip);
291
getlong(sgip); /* Minimum pixel */
292
getlong(sgip); /* Maximum pixel */
297
* This file is compressed; read the scanline tables...
300
fseek(sgip->file, 512, SEEK_SET);
302
sgip->table = calloc(sgip->zsize, sizeof(long *));
303
sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
304
for (i = 1; i < sgip->zsize; i ++)
305
sgip->table[i] = sgip->table[0] + i * sgip->ysize;
307
for (i = 0; i < sgip->zsize; i ++)
308
for (j = 0; j < sgip->ysize; j ++)
309
sgip->table[i][j] = getlong(sgip);
317
bpp < 1 || bpp > 2 ||
318
comp < SGI_COMP_NONE || comp > SGI_COMP_ARLE)
324
sgip->mode = SGI_WRITE;
326
putshort(SGI_MAGIC, sgip);
327
putc((sgip->comp = comp) != 0, sgip->file);
328
putc(sgip->bpp = bpp, sgip->file);
329
putshort(3, sgip); /* Dimensions */
330
putshort(sgip->xsize = xsize, sgip);
331
putshort(sgip->ysize = ysize, sgip);
332
putshort(sgip->zsize = zsize, sgip);
335
putlong(0, sgip); /* Minimum pixel */
336
putlong(255, sgip); /* Maximum pixel */
340
putlong(-32768, sgip); /* Minimum pixel */
341
putlong(32767, sgip); /* Maximum pixel */
343
putlong(0, sgip); /* Reserved */
345
memset(name, 0, sizeof(name));
346
fwrite(name, sizeof(name), 1, sgip->file);
348
for (i = 0; i < 102; i ++)
353
case SGI_COMP_NONE : /* No compression */
355
* This file is uncompressed. To avoid problems with sparse files,
356
* we need to write blank pixels for the entire image...
361
for (i = xsize * ysize * zsize; i > 0; i --)
366
for (i = xsize * ysize * zsize; i > 0; i --)
371
case SGI_COMP_ARLE : /* Aggressive RLE */
372
sgip->arle_row = (unsigned short *)calloc(xsize, sizeof(unsigned short));
373
sgip->arle_offset = 0;
375
case SGI_COMP_RLE : /* Run-Length Encoding */
377
* This file is compressed; write the (blank) scanline tables...
380
for (i = 2 * ysize * zsize; i > 0; i --)
383
sgip->firstrow = ftell(sgip->file);
384
sgip->nextrow = ftell(sgip->file);
385
sgip->table = calloc(sgip->zsize, sizeof(long *));
386
sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
387
for (i = 1; i < sgip->zsize; i ++)
388
sgip->table[i] = sgip->table[0] + i * sgip->ysize;
389
sgip->length = calloc(sgip->zsize, sizeof(long *));
390
sgip->length[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
391
for (i = 1; i < sgip->zsize; i ++)
392
sgip->length[i] = sgip->length[0] + i * sgip->ysize;
407
* 'sgiPutRow()' - Put a row of image data to a file.
411
sgiPutRow(sgi_t *sgip, /* I - SGI image */
412
unsigned short *row, /* I - Row to write */
413
int y, /* I - Line to write */
414
int z) /* I - Channel to write */
416
int x; /* X coordinate */
417
long offset; /* File offset */
422
y < 0 || y >= sgip->ysize ||
423
z < 0 || z >= sgip->zsize)
430
* Seek to the image row - optimize buffering by only seeking if
434
offset = 512 + (y + z * sgip->ysize) * sgip->xsize * sgip->bpp;
435
if (offset != ftell(sgip->file))
436
fseek(sgip->file, offset, SEEK_SET);
440
for (x = sgip->xsize; x > 0; x --, row ++)
441
putc(*row, sgip->file);
445
for (x = sgip->xsize; x > 0; x --, row ++)
446
putshort(*row, sgip);
451
if (sgip->table[z][y] != 0)
455
* First check the last row written...
458
if (sgip->arle_offset > 0)
460
for (x = 0; x < sgip->xsize; x ++)
461
if (row[x] != sgip->arle_row[x])
464
if (x == sgip->xsize)
466
sgip->table[z][y] = sgip->arle_offset;
467
sgip->length[z][y] = sgip->arle_length;
473
* If that didn't match, search all the previous rows...
476
fseek(sgip->file, sgip->firstrow, SEEK_SET);
482
sgip->arle_offset = ftell(sgip->file);
483
if ((sgip->arle_length = read_rle8(sgip, sgip->arle_row, sgip->xsize)) < 0)
489
for (x = 0; x < sgip->xsize; x ++)
490
if (row[x] != sgip->arle_row[x])
493
while (x < sgip->xsize);
499
sgip->arle_offset = ftell(sgip->file);
500
if ((sgip->arle_length = read_rle16(sgip, sgip->arle_row, sgip->xsize)) < 0)
506
for (x = 0; x < sgip->xsize; x ++)
507
if (row[x] != sgip->arle_row[x])
510
while (x < sgip->xsize);
513
if (x == sgip->xsize)
515
sgip->table[z][y] = sgip->arle_offset;
516
sgip->length[z][y] = sgip->arle_length;
520
fseek(sgip->file, 0, SEEK_END); /* Clear EOF */
523
if (sgip->table[z][y] != 0)
526
offset = sgip->table[z][y] = sgip->nextrow;
528
if (offset != ftell(sgip->file))
529
fseek(sgip->file, offset, SEEK_SET);
532
x = write_rle8(sgip, row, sgip->xsize);
534
x = write_rle16(sgip, row, sgip->xsize);
536
if (sgip->comp == SGI_COMP_ARLE)
538
sgip->arle_offset = offset;
539
sgip->arle_length = x;
540
memcpy(sgip->arle_row, row, sgip->xsize * sizeof(short));
543
sgip->nextrow = ftell(sgip->file);
544
sgip->length[z][y] = x;
554
* 'getlong()' - Get a 32-bit big-endian integer.
558
getlong(sgi_t *sgip) /* I - SGI image to read from */
563
fread(b, 4, 1, sgip->file);
565
return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
567
return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
572
* 'getshort()' - Get a 16-bit big-endian integer.
576
getshort(sgi_t *sgip) /* I - SGI image to read from */
581
fread(b, 2, 1, sgip->file);
583
return ((b[1] << 8) | b[0]);
585
return ((b[0] << 8) | b[1]);
590
* 'putlong()' - Put a 32-bit big-endian integer.
594
putlong(long n, /* I - Long to write */
595
sgi_t *sgip) /* I - File to write to */
597
if (putc(n >> 24, sgip->file) == EOF)
599
if (putc(n >> 16, sgip->file) == EOF)
601
if (putc(n >> 8, sgip->file) == EOF)
603
if (putc(n, sgip->file) == EOF)
611
* 'putshort()' - Put a 16-bit big-endian integer.
615
putshort(unsigned short n, /* I - Short to write */
616
sgi_t *sgip) /* I - File to write to */
618
if (putc(n >> 8, sgip->file) == EOF)
620
if (putc(n, sgip->file) == EOF)
628
* 'read_rle8()' - Read 8-bit RLE data.
632
read_rle8(sgi_t *sgip, /* I - SGI image to read from */
633
unsigned short *row, /* O - Data */
634
int xsize) /* I - Width of data in pixels */
636
int i, /* Looping var */
637
ch, /* Current character */
638
count, /* RLE count */
639
length; /* Number of bytes read... */
646
if ((ch = getc(sgip->file)) == EOF)
656
for (i = 0; i < count; i ++, row ++, xsize --, length ++)
657
*row = getc(sgip->file);
661
ch = getc(sgip->file);
663
for (i = 0; i < count; i ++, row ++, xsize --)
668
return (xsize > 0 ? -1 : length);
673
* 'read_rle16()' - Read 16-bit RLE data.
677
read_rle16(sgi_t *sgip, /* I - SGI image to read from */
678
unsigned short *row, /* O - Data */
679
int xsize)/* I - Width of data in pixels */
681
int i, /* Looping var */
682
ch, /* Current character */
683
count, /* RLE count */
684
length; /* Number of bytes read... */
691
if ((ch = getshort(sgip)) == EOF)
701
for (i = 0; i < count; i ++, row ++, xsize --, length ++)
702
*row = getshort(sgip);
708
for (i = 0; i < count; i ++, row ++, xsize --)
713
return (xsize > 0 ? -1 : length * 2);
718
* 'write_rle8()' - Write 8-bit RLE data.
722
write_rle8(sgi_t *sgip, /* I - SGI image to write to */
723
unsigned short *row, /* I - Data */
724
int xsize)/* I - Width of data in pixels */
726
int length, /* Length of output line */
727
count, /* Number of repeated/non-repeated pixels */
730
unsigned short *start, /* Start of sequence */
731
repeat; /* Repeated pixel */
734
for (x = xsize, length = 0; x > 0;)
740
while (x > 0 && (row[-2] != row[-1] || row[-1] != row[0]))
752
i = count > 126 ? 126 : count;
755
if (putc(128 | i, sgip->file) == EOF)
761
if (putc(*start, sgip->file) == EOF)
778
while (x > 0 && *row == repeat)
787
i = count > 126 ? 126 : count;
790
if (putc(i, sgip->file) == EOF)
794
if (putc(repeat, sgip->file) == EOF)
802
if (putc(0, sgip->file) == EOF)
810
* 'write_rle16()' - Write 16-bit RLE data.
814
write_rle16(sgi_t *sgip, /* I - SGI image to write to */
815
unsigned short *row,/* I - Data */
816
int xsize)/* I - Width of data in pixels */
818
int length, /* Length of output line */
819
count, /* Number of repeated/non-repeated pixels */
822
unsigned short *start, /* Start of sequence */
823
repeat; /* Repeated pixel */
826
for (x = xsize, length = 0; x > 0;)
832
while (x > 0 && (row[-2] != row[-1] || row[-1] != row[0]))
844
i = count > 126 ? 126 : count;
847
if (putshort(128 | i, sgip) == EOF)
853
if (putshort(*start, sgip) == EOF)
870
while (x > 0 && *row == repeat)
879
i = count > 126 ? 126 : count;
882
if (putshort(i, sgip) == EOF)
886
if (putshort(repeat, sgip) == EOF)
894
if (putshort(0, sgip) == EOF)
902
* End of "$Id: sgilib.c,v 1.8 2003/04/07 11:59:33 neo Exp $".