]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-sgilib.c
2 * "$Id: image-sgilib.c,v 1.4 2000/01/04 13:45:45 mike Exp $"
4 * SGI image file format library routines for the Common UNIX Printing
7 * Copyright 1993-2000 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636-3111 USA
21 * Voice: (301) 373-9603
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
27 * sgiClose() - Close an SGI image file.
28 * sgiGetRow() - Get a row of image data from a file.
29 * sgiOpen() - Open an SGI image file for reading or writing.
30 * sgiOpenFile() - Open an SGI image file for reading or writing.
31 * sgiPutRow() - Put a row of image data to a file.
32 * getlong() - Get a 32-bit big-endian integer.
33 * getshort() - Get a 16-bit big-endian integer.
34 * putlong() - Put a 32-bit big-endian integer.
35 * putshort() - Put a 16-bit big-endian integer.
36 * read_rle8() - Read 8-bit RLE data.
37 * read_rle16() - Read 16-bit RLE data.
38 * write_rle8() - Write 8-bit RLE data.
39 * write_rle16() - Write 16-bit RLE data.
42 #include "image-sgi.h"
49 static int getlong(FILE *);
50 static int getshort(FILE *);
51 static int putlong(long, FILE *);
52 static int putshort(unsigned short, FILE *);
53 static int read_rle8(FILE *, unsigned short *, int);
54 static int read_rle16(FILE *, unsigned short *, int);
55 static int write_rle8(FILE *, unsigned short *, int);
56 static int write_rle16(FILE *, unsigned short *, int);
60 * 'sgiClose()' - Close an SGI image file.
64 sgiClose(sgi_t
*sgip
) /* I - SGI image */
66 int i
; /* Return status */
67 long *offset
; /* Looping var for offset table */
73 if (sgip
->mode
== SGI_WRITE
&& sgip
->comp
!= SGI_COMP_NONE
)
76 * Write the scanline offset table to the file...
79 fseek(sgip
->file
, 512, SEEK_SET
);
81 for (i
= sgip
->ysize
* sgip
->zsize
, offset
= sgip
->table
[0];
84 if (putlong(offset
[0], sgip
->file
) < 0)
87 for (i
= sgip
->ysize
* sgip
->zsize
, offset
= sgip
->length
[0];
90 if (putlong(offset
[0], sgip
->file
) < 0)
94 if (sgip
->table
!= NULL
)
100 if (sgip
->length
!= NULL
)
102 free(sgip
->length
[0]);
106 if (sgip
->comp
== SGI_COMP_ARLE
)
107 free(sgip
->arle_row
);
109 i
= fclose(sgip
->file
);
117 * 'sgiGetRow()' - Get a row of image data from a file.
121 sgiGetRow(sgi_t
*sgip
, /* I - SGI image */
122 unsigned short *row
, /* O - Row to read */
123 int y
, /* I - Line to read */
124 int z
) /* I - Channel to read */
126 int x
; /* X coordinate */
127 long offset
; /* File offset */
132 y
< 0 || y
>= sgip
->ysize
||
133 z
< 0 || z
>= sgip
->zsize
)
140 * Seek to the image row - optimize buffering by only seeking if
144 offset
= 512 + (y
+ z
* sgip
->ysize
) * sgip
->xsize
* sgip
->bpp
;
145 if (offset
!= ftell(sgip
->file
))
146 fseek(sgip
->file
, offset
, SEEK_SET
);
150 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
151 *row
= getc(sgip
->file
);
155 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
156 *row
= getshort(sgip
->file
);
161 offset
= sgip
->table
[z
][y
];
162 if (offset
!= ftell(sgip
->file
))
163 fseek(sgip
->file
, offset
, SEEK_SET
);
166 return (read_rle8(sgip
->file
, row
, sgip
->xsize
));
168 return (read_rle16(sgip
->file
, row
, sgip
->xsize
));
176 * 'sgiOpen()' - Open an SGI image file for reading or writing.
180 sgiOpen(char *filename
, /* I - File to open */
181 int mode
, /* I - Open mode (SGI_READ or SGI_WRITE) */
182 int comp
, /* I - Type of compression */
183 int bpp
, /* I - Bytes per pixel */
184 int xsize
, /* I - Width of image in pixels */
185 int ysize
, /* I - Height of image in pixels */
186 int zsize
) /* I - Number of channels */
188 sgi_t
*sgip
; /* New SGI image file */
189 FILE *file
; /* Image file pointer */
192 if (mode
== SGI_READ
)
193 file
= fopen(filename
, "rb");
195 file
= fopen(filename
, "wb+");
200 if ((sgip
= sgiOpenFile(file
, mode
, comp
, bpp
, xsize
, ysize
, zsize
)) == NULL
)
208 * 'sgiOpenFile()' - Open an SGI image file for reading or writing.
212 sgiOpenFile(FILE *file
, /* I - File to open */
213 int mode
, /* I - Open mode (SGI_READ or SGI_WRITE) */
214 int comp
, /* I - Type of compression */
215 int bpp
, /* I - Bytes per pixel */
216 int xsize
, /* I - Width of image in pixels */
217 int ysize
, /* I - Height of image in pixels */
218 int zsize
) /* I - Number of channels */
220 int i
, j
; /* Looping var */
221 char name
[80]; /* Name of file in image header */
222 short magic
; /* Magic number */
223 sgi_t
*sgip
; /* New image pointer */
226 if ((sgip
= calloc(sizeof(sgi_t
), 1)) == NULL
)
234 sgip
->mode
= SGI_READ
;
236 magic
= getshort(sgip
->file
);
237 if (magic
!= SGI_MAGIC
)
243 sgip
->comp
= getc(sgip
->file
);
244 sgip
->bpp
= getc(sgip
->file
);
245 getshort(sgip
->file
); /* Dimensions */
246 sgip
->xsize
= getshort(sgip
->file
);
247 sgip
->ysize
= getshort(sgip
->file
);
248 sgip
->zsize
= getshort(sgip
->file
);
249 getlong(sgip
->file
); /* Minimum pixel */
250 getlong(sgip
->file
); /* Maximum pixel */
255 * This file is compressed; read the scanline tables...
258 fseek(sgip
->file
, 512, SEEK_SET
);
260 sgip
->table
= calloc(sgip
->zsize
, sizeof(long *));
261 sgip
->table
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
262 for (i
= 1; i
< sgip
->zsize
; i
++)
263 sgip
->table
[i
] = sgip
->table
[0] + i
* sgip
->ysize
;
265 for (i
= 0; i
< sgip
->zsize
; i
++)
266 for (j
= 0; j
< sgip
->ysize
; j
++)
267 sgip
->table
[i
][j
] = getlong(sgip
->file
);
275 bpp
< 1 || bpp
> 2 ||
276 comp
< SGI_COMP_NONE
|| comp
> SGI_COMP_ARLE
)
282 sgip
->mode
= SGI_WRITE
;
284 putshort(SGI_MAGIC
, sgip
->file
);
285 putc((sgip
->comp
= comp
) != 0, sgip
->file
);
286 putc(sgip
->bpp
= bpp
, sgip
->file
);
287 putshort(3, sgip
->file
); /* Dimensions */
288 putshort(sgip
->xsize
= xsize
, sgip
->file
);
289 putshort(sgip
->ysize
= ysize
, sgip
->file
);
290 putshort(sgip
->zsize
= zsize
, sgip
->file
);
293 putlong(0, sgip
->file
); /* Minimum pixel */
294 putlong(255, sgip
->file
); /* Maximum pixel */
298 putlong(-32768, sgip
->file
); /* Minimum pixel */
299 putlong(32767, sgip
->file
); /* Maximum pixel */
301 putlong(0, sgip
->file
); /* Reserved */
303 memset(name
, 0, sizeof(name
));
304 fwrite(name
, sizeof(name
), 1, sgip
->file
);
306 for (i
= 0; i
< 102; i
++)
307 putlong(0, sgip
->file
);
311 case SGI_COMP_NONE
: /* No compression */
313 * This file is uncompressed. To avoid problems with sparse files,
314 * we need to write blank pixels for the entire image...
319 for (i
= xsize
* ysize
* zsize
; i
> 0; i
--)
324 for (i
= xsize
* ysize
* zsize
; i
> 0; i
--)
325 putshort(0, sgip
->file
);
329 case SGI_COMP_ARLE
: /* Aggressive RLE */
330 sgip
->arle_row
= calloc(xsize
, sizeof(unsigned short));
331 sgip
->arle_offset
= 0;
333 case SGI_COMP_RLE
: /* Run-Length Encoding */
335 * This file is compressed; write the (blank) scanline tables...
338 for (i
= 2 * ysize
* zsize
; i
> 0; i
--)
339 putlong(0, sgip
->file
);
341 sgip
->firstrow
= ftell(sgip
->file
);
342 sgip
->nextrow
= ftell(sgip
->file
);
343 sgip
->table
= calloc(sgip
->zsize
, sizeof(long *));
344 sgip
->table
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
345 for (i
= 1; i
< sgip
->zsize
; i
++)
346 sgip
->table
[i
] = sgip
->table
[0] + i
* sgip
->ysize
;
347 sgip
->length
= calloc(sgip
->zsize
, sizeof(long *));
348 sgip
->length
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
349 for (i
= 1; i
< sgip
->zsize
; i
++)
350 sgip
->length
[i
] = sgip
->length
[0] + i
* sgip
->ysize
;
365 * 'sgiPutRow()' - Put a row of image data to a file.
369 sgiPutRow(sgi_t
*sgip
, /* I - SGI image */
370 unsigned short *row
, /* I - Row to write */
371 int y
, /* I - Line to write */
372 int z
) /* I - Channel to write */
374 int x
; /* X coordinate */
375 long offset
; /* File offset */
380 y
< 0 || y
>= sgip
->ysize
||
381 z
< 0 || z
>= sgip
->zsize
)
388 * Seek to the image row - optimize buffering by only seeking if
392 offset
= 512 + (y
+ z
* sgip
->ysize
) * sgip
->xsize
* sgip
->bpp
;
393 if (offset
!= ftell(sgip
->file
))
394 fseek(sgip
->file
, offset
, SEEK_SET
);
398 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
399 putc(*row
, sgip
->file
);
403 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
404 putshort(*row
, sgip
->file
);
409 if (sgip
->table
[z
][y
] != 0)
413 * First check the last row written...
416 if (sgip
->arle_offset
> 0)
418 for (x
= 0; x
< sgip
->xsize
; x
++)
419 if (row
[x
] != sgip
->arle_row
[x
])
422 if (x
== sgip
->xsize
)
424 sgip
->table
[z
][y
] = sgip
->arle_offset
;
425 sgip
->length
[z
][y
] = sgip
->arle_length
;
431 * If that didn't match, search all the previous rows...
434 fseek(sgip
->file
, sgip
->firstrow
, SEEK_SET
);
440 sgip
->arle_offset
= ftell(sgip
->file
);
441 if ((sgip
->arle_length
= read_rle8(sgip
->file
, sgip
->arle_row
, sgip
->xsize
)) < 0)
447 if (memcmp(row
, sgip
->arle_row
, sgip
->xsize
* sizeof(unsigned short)) == 0)
458 sgip
->arle_offset
= ftell(sgip
->file
);
459 if ((sgip
->arle_length
= read_rle16(sgip
->file
, sgip
->arle_row
, sgip
->xsize
)) < 0)
465 if (memcmp(row
, sgip
->arle_row
, sgip
->xsize
* sizeof(unsigned short)) == 0)
473 if (x
== sgip
->xsize
)
475 sgip
->table
[z
][y
] = sgip
->arle_offset
;
476 sgip
->length
[z
][y
] = sgip
->arle_length
;
480 fseek(sgip
->file
, 0, SEEK_END
); /* Clear EOF */
483 if (sgip
->table
[z
][y
] != 0)
486 offset
= sgip
->table
[z
][y
] = sgip
->nextrow
;
488 if (offset
!= ftell(sgip
->file
))
489 fseek(sgip
->file
, offset
, SEEK_SET
);
492 x
= write_rle8(sgip
->file
, row
, sgip
->xsize
);
494 x
= write_rle16(sgip
->file
, row
, sgip
->xsize
);
496 if (sgip
->comp
== SGI_COMP_ARLE
)
498 sgip
->arle_offset
= offset
;
499 sgip
->arle_length
= x
;
500 memcpy(sgip
->arle_row
, row
, sgip
->xsize
* sizeof(unsigned short));
503 sgip
->nextrow
= ftell(sgip
->file
);
504 sgip
->length
[z
][y
] = x
;
514 * 'getlong()' - Get a 32-bit big-endian integer.
518 getlong(FILE *fp
) /* I - File to read from */
524 return ((b
[0] << 24) | (b
[1] << 16) | (b
[2] << 8) | b
[3]);
529 * 'getshort()' - Get a 16-bit big-endian integer.
533 getshort(FILE *fp
) /* I - File to read from */
539 return ((b
[0] << 8) | b
[1]);
544 * 'putlong()' - Put a 32-bit big-endian integer.
548 putlong(long n
, /* I - Long to write */
549 FILE *fp
) /* I - File to write to */
551 if (putc(n
>> 24, fp
) == EOF
)
553 if (putc(n
>> 16, fp
) == EOF
)
555 if (putc(n
>> 8, fp
) == EOF
)
557 if (putc(n
, fp
) == EOF
)
565 * 'putshort()' - Put a 16-bit big-endian integer.
569 putshort(unsigned short n
, /* I - Short to write */
570 FILE *fp
) /* I - File to write to */
572 if (putc(n
>> 8, fp
) == EOF
)
574 if (putc(n
, fp
) == EOF
)
582 * 'read_rle8()' - Read 8-bit RLE data.
586 read_rle8(FILE *fp
, /* I - File to read from */
587 unsigned short *row
, /* O - Data */
588 int xsize
) /* I - Width of data in pixels */
590 int i
, /* Looping var */
591 ch
, /* Current character */
592 count
, /* RLE count */
593 length
; /* Number of bytes read... */
600 if ((ch
= getc(fp
)) == EOF
)
610 for (i
= 0; i
< count
; i
++, row
++, xsize
--, length
++)
617 for (i
= 0; i
< count
; i
++, row
++, xsize
--)
622 return (xsize
> 0 ? -1 : length
);
627 * 'read_rle16()' - Read 16-bit RLE data.
631 read_rle16(FILE *fp
, /* I - File to read from */
632 unsigned short *row
, /* O - Data */
633 int xsize
)/* I - Width of data in pixels */
635 int i
, /* Looping var */
636 ch
, /* Current character */
637 count
, /* RLE count */
638 length
; /* Number of bytes read... */
645 if ((ch
= getshort(fp
)) == EOF
)
655 for (i
= 0; i
< count
; i
++, row
++, xsize
--, length
++)
662 for (i
= 0; i
< count
; i
++, row
++, xsize
--)
667 return (xsize
> 0 ? -1 : length
* 2);
672 * 'write_rle8()' - Write 8-bit RLE data.
676 write_rle8(FILE *fp
, /* I - File to write to */
677 unsigned short *row
, /* I - Data */
678 int xsize
)/* I - Width of data in pixels */
684 unsigned short *start
,
688 for (x
= xsize
, length
= 0; x
> 0;)
694 while (x
> 0 && (row
[-2] != row
[-1] || row
[-1] != row
[0]))
706 i
= count
> 126 ? 126 : count
;
709 if (putc(128 | i
, fp
) == EOF
)
715 if (putc(*start
, fp
) == EOF
)
732 while (x
> 0 && *row
== repeat
)
741 i
= count
> 126 ? 126 : count
;
744 if (putc(i
, fp
) == EOF
)
748 if (putc(repeat
, fp
) == EOF
)
756 if (putc(0, fp
) == EOF
)
764 * 'write_rle16()' - Write 16-bit RLE data.
768 write_rle16(FILE *fp
, /* I - File to write to */
769 unsigned short *row
, /* I - Data */
770 int xsize
)/* I - Width of data in pixels */
776 unsigned short *start
,
780 for (x
= xsize
, length
= 0; x
> 0;)
786 while (x
> 0 && (row
[-2] != row
[-1] || row
[-1] != row
[0]))
798 i
= count
> 126 ? 126 : count
;
801 if (putshort(128 | i
, fp
) == EOF
)
807 if (putshort(*start
, fp
) == EOF
)
824 while (x
> 0 && *row
== repeat
)
833 i
= count
> 126 ? 126 : count
;
836 if (putshort(i
, fp
) == EOF
)
840 if (putshort(repeat
, fp
) == EOF
)
848 if (putshort(0, fp
) == EOF
)
856 * End of "$Id: image-sgilib.c,v 1.4 2000/01/04 13:45:45 mike Exp $".