]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-sgilib.c
2 * "$Id: image-sgilib.c 6649 2007-07-11 21:46:42Z mike $"
4 * SGI image file format library routines for the Common UNIX Printing
7 * Copyright 2007 by Apple Inc.
8 * Copyright 1993-2005 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * sgiClose() - Close an SGI image file.
21 * sgiGetRow() - Get a row of image data from a file.
22 * sgiOpen() - Open an SGI image file for reading or writing.
23 * sgiOpenFile() - Open an SGI image file for reading or writing.
24 * sgiPutRow() - Put a row of image data to a file.
25 * getlong() - Get a 32-bit big-endian integer.
26 * getshort() - Get a 16-bit big-endian integer.
27 * putlong() - Put a 32-bit big-endian integer.
28 * putshort() - Put a 16-bit big-endian integer.
29 * read_rle8() - Read 8-bit RLE data.
30 * read_rle16() - Read 16-bit RLE data.
31 * write_rle8() - Write 8-bit RLE data.
32 * write_rle16() - Write 16-bit RLE data.
35 #include "image-sgi.h"
42 static int getlong(FILE *);
43 static int getshort(FILE *);
44 static int putlong(long, FILE *);
45 static int putshort(unsigned short, FILE *);
46 static int read_rle8(FILE *, unsigned short *, int);
47 static int read_rle16(FILE *, unsigned short *, int);
48 static int write_rle8(FILE *, unsigned short *, int);
49 static int write_rle16(FILE *, unsigned short *, int);
53 * 'sgiClose()' - Close an SGI image file.
56 int /* O - 0 on success, -1 on error */
57 sgiClose(sgi_t
*sgip
) /* I - SGI image */
59 int i
; /* Return status */
60 long *offset
; /* Looping var for offset table */
66 if (sgip
->mode
== SGI_WRITE
&& sgip
->comp
!= SGI_COMP_NONE
)
69 * Write the scanline offset table to the file...
72 fseek(sgip
->file
, 512, SEEK_SET
);
74 for (i
= sgip
->ysize
* sgip
->zsize
, offset
= sgip
->table
[0];
77 if (putlong(offset
[0], sgip
->file
) < 0)
80 for (i
= sgip
->ysize
* sgip
->zsize
, offset
= sgip
->length
[0];
83 if (putlong(offset
[0], sgip
->file
) < 0)
87 if (sgip
->table
!= NULL
)
93 if (sgip
->length
!= NULL
)
95 free(sgip
->length
[0]);
99 if (sgip
->comp
== SGI_COMP_ARLE
)
100 free(sgip
->arle_row
);
102 i
= fclose(sgip
->file
);
110 * 'sgiGetRow()' - Get a row of image data from a file.
113 int /* O - 0 on success, -1 on error */
114 sgiGetRow(sgi_t
*sgip
, /* I - SGI image */
115 unsigned short *row
, /* O - Row to read */
116 int y
, /* I - Line to read */
117 int z
) /* I - Channel to read */
119 int x
; /* X coordinate */
120 long offset
; /* File offset */
125 y
< 0 || y
>= sgip
->ysize
||
126 z
< 0 || z
>= sgip
->zsize
)
133 * Seek to the image row - optimize buffering by only seeking if
137 offset
= 512 + (y
+ z
* sgip
->ysize
) * sgip
->xsize
* sgip
->bpp
;
138 if (offset
!= ftell(sgip
->file
))
139 fseek(sgip
->file
, offset
, SEEK_SET
);
143 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
144 *row
= getc(sgip
->file
);
148 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
149 *row
= getshort(sgip
->file
);
154 offset
= sgip
->table
[z
][y
];
155 if (offset
!= ftell(sgip
->file
))
156 fseek(sgip
->file
, offset
, SEEK_SET
);
159 return (read_rle8(sgip
->file
, row
, sgip
->xsize
));
161 return (read_rle16(sgip
->file
, row
, sgip
->xsize
));
169 * 'sgiOpen()' - Open an SGI image file for reading or writing.
172 sgi_t
* /* O - New image */
173 sgiOpen(const char *filename
, /* I - File to open */
174 int mode
, /* I - Open mode (SGI_READ or SGI_WRITE) */
175 int comp
, /* I - Type of compression */
176 int bpp
, /* I - Bytes per pixel */
177 int xsize
, /* I - Width of image in pixels */
178 int ysize
, /* I - Height of image in pixels */
179 int zsize
) /* I - Number of channels */
181 sgi_t
*sgip
; /* New SGI image file */
182 FILE *file
; /* Image file pointer */
185 if (mode
== SGI_READ
)
186 file
= fopen(filename
, "rb");
188 file
= fopen(filename
, "wb+");
193 if ((sgip
= sgiOpenFile(file
, mode
, comp
, bpp
, xsize
, ysize
, zsize
)) == NULL
)
201 * 'sgiOpenFile()' - Open an SGI image file for reading or writing.
204 sgi_t
* /* O - New image */
205 sgiOpenFile(FILE *file
, /* I - File to open */
206 int mode
, /* I - Open mode (SGI_READ or SGI_WRITE) */
207 int comp
, /* I - Type of compression */
208 int bpp
, /* I - Bytes per pixel */
209 int xsize
, /* I - Width of image in pixels */
210 int ysize
, /* I - Height of image in pixels */
211 int zsize
) /* I - Number of channels */
213 int i
, j
; /* Looping var */
214 char name
[80]; /* Name of file in image header */
215 short magic
; /* Magic number */
216 sgi_t
*sgip
; /* New image pointer */
219 if ((sgip
= calloc(sizeof(sgi_t
), 1)) == NULL
)
227 sgip
->mode
= SGI_READ
;
229 magic
= getshort(sgip
->file
);
230 if (magic
!= SGI_MAGIC
)
236 sgip
->comp
= getc(sgip
->file
);
237 sgip
->bpp
= getc(sgip
->file
);
238 getshort(sgip
->file
); /* Dimensions */
239 sgip
->xsize
= getshort(sgip
->file
);
240 sgip
->ysize
= getshort(sgip
->file
);
241 sgip
->zsize
= getshort(sgip
->file
);
242 getlong(sgip
->file
); /* Minimum pixel */
243 getlong(sgip
->file
); /* Maximum pixel */
248 * This file is compressed; read the scanline tables...
251 fseek(sgip
->file
, 512, SEEK_SET
);
253 sgip
->table
= calloc(sgip
->zsize
, sizeof(long *));
254 sgip
->table
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
255 for (i
= 1; i
< sgip
->zsize
; i
++)
256 sgip
->table
[i
] = sgip
->table
[0] + i
* sgip
->ysize
;
258 for (i
= 0; i
< sgip
->zsize
; i
++)
259 for (j
= 0; j
< sgip
->ysize
; j
++)
260 sgip
->table
[i
][j
] = getlong(sgip
->file
);
268 bpp
< 1 || bpp
> 2 ||
269 comp
< SGI_COMP_NONE
|| comp
> SGI_COMP_ARLE
)
275 sgip
->mode
= SGI_WRITE
;
277 putshort(SGI_MAGIC
, sgip
->file
);
278 putc((sgip
->comp
= comp
) != 0, sgip
->file
);
279 putc(sgip
->bpp
= bpp
, sgip
->file
);
280 putshort(3, sgip
->file
); /* Dimensions */
281 putshort(sgip
->xsize
= xsize
, sgip
->file
);
282 putshort(sgip
->ysize
= ysize
, sgip
->file
);
283 putshort(sgip
->zsize
= zsize
, sgip
->file
);
286 putlong(0, sgip
->file
); /* Minimum pixel */
287 putlong(255, sgip
->file
); /* Maximum pixel */
291 putlong(-32768, sgip
->file
); /* Minimum pixel */
292 putlong(32767, sgip
->file
); /* Maximum pixel */
294 putlong(0, sgip
->file
); /* Reserved */
296 memset(name
, 0, sizeof(name
));
297 fwrite(name
, sizeof(name
), 1, sgip
->file
);
299 for (i
= 0; i
< 102; i
++)
300 putlong(0, sgip
->file
);
304 case SGI_COMP_NONE
: /* No compression */
306 * This file is uncompressed. To avoid problems with sparse files,
307 * we need to write blank pixels for the entire image...
312 for (i
= xsize
* ysize
* zsize
; i
> 0; i
--)
317 for (i
= xsize
* ysize
* zsize
; i
> 0; i
--)
318 putshort(0, sgip
->file
);
322 case SGI_COMP_ARLE
: /* Aggressive RLE */
323 sgip
->arle_row
= calloc(xsize
, sizeof(unsigned short));
324 sgip
->arle_offset
= 0;
326 case SGI_COMP_RLE
: /* Run-Length Encoding */
328 * This file is compressed; write the (blank) scanline tables...
331 for (i
= 2 * ysize
* zsize
; i
> 0; i
--)
332 putlong(0, sgip
->file
);
334 sgip
->firstrow
= ftell(sgip
->file
);
335 sgip
->nextrow
= ftell(sgip
->file
);
336 sgip
->table
= calloc(sgip
->zsize
, sizeof(long *));
337 sgip
->table
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
338 for (i
= 1; i
< sgip
->zsize
; i
++)
339 sgip
->table
[i
] = sgip
->table
[0] + i
* sgip
->ysize
;
340 sgip
->length
= calloc(sgip
->zsize
, sizeof(long *));
341 sgip
->length
[0] = calloc(sgip
->ysize
* sgip
->zsize
, sizeof(long));
342 for (i
= 1; i
< sgip
->zsize
; i
++)
343 sgip
->length
[i
] = sgip
->length
[0] + i
* sgip
->ysize
;
358 * 'sgiPutRow()' - Put a row of image data to a file.
361 int /* O - 0 on success, -1 on error */
362 sgiPutRow(sgi_t
*sgip
, /* I - SGI image */
363 unsigned short *row
, /* I - Row to write */
364 int y
, /* I - Line to write */
365 int z
) /* I - Channel to write */
367 int x
; /* X coordinate */
368 long offset
; /* File offset */
373 y
< 0 || y
>= sgip
->ysize
||
374 z
< 0 || z
>= sgip
->zsize
)
381 * Seek to the image row - optimize buffering by only seeking if
385 offset
= 512 + (y
+ z
* sgip
->ysize
) * sgip
->xsize
* sgip
->bpp
;
386 if (offset
!= ftell(sgip
->file
))
387 fseek(sgip
->file
, offset
, SEEK_SET
);
391 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
392 putc(*row
, sgip
->file
);
396 for (x
= sgip
->xsize
; x
> 0; x
--, row
++)
397 putshort(*row
, sgip
->file
);
402 if (sgip
->table
[z
][y
] != 0)
406 * First check the last row written...
409 if (sgip
->arle_offset
> 0)
411 for (x
= 0; x
< sgip
->xsize
; x
++)
412 if (row
[x
] != sgip
->arle_row
[x
])
415 if (x
== sgip
->xsize
)
417 sgip
->table
[z
][y
] = sgip
->arle_offset
;
418 sgip
->length
[z
][y
] = sgip
->arle_length
;
424 * If that didn't match, search all the previous rows...
427 fseek(sgip
->file
, sgip
->firstrow
, SEEK_SET
);
433 sgip
->arle_offset
= ftell(sgip
->file
);
434 if ((sgip
->arle_length
= read_rle8(sgip
->file
, sgip
->arle_row
, sgip
->xsize
)) < 0)
440 if (memcmp(row
, sgip
->arle_row
, sgip
->xsize
* sizeof(unsigned short)) == 0)
451 sgip
->arle_offset
= ftell(sgip
->file
);
452 if ((sgip
->arle_length
= read_rle16(sgip
->file
, sgip
->arle_row
, sgip
->xsize
)) < 0)
458 if (memcmp(row
, sgip
->arle_row
, sgip
->xsize
* sizeof(unsigned short)) == 0)
466 if (x
== sgip
->xsize
)
468 sgip
->table
[z
][y
] = sgip
->arle_offset
;
469 sgip
->length
[z
][y
] = sgip
->arle_length
;
473 fseek(sgip
->file
, 0, SEEK_END
); /* Clear EOF */
476 if (sgip
->table
[z
][y
] != 0)
479 offset
= sgip
->table
[z
][y
] = sgip
->nextrow
;
481 if (offset
!= ftell(sgip
->file
))
482 fseek(sgip
->file
, offset
, SEEK_SET
);
485 x
= write_rle8(sgip
->file
, row
, sgip
->xsize
);
487 x
= write_rle16(sgip
->file
, row
, sgip
->xsize
);
489 if (sgip
->comp
== SGI_COMP_ARLE
)
491 sgip
->arle_offset
= offset
;
492 sgip
->arle_length
= x
;
493 memcpy(sgip
->arle_row
, row
, sgip
->xsize
* sizeof(unsigned short));
496 sgip
->nextrow
= ftell(sgip
->file
);
497 sgip
->length
[z
][y
] = x
;
507 * 'getlong()' - Get a 32-bit big-endian integer.
510 static int /* O - Long value */
511 getlong(FILE *fp
) /* I - File to read from */
513 unsigned char b
[4]; /* Bytes from file */
517 return ((b
[0] << 24) | (b
[1] << 16) | (b
[2] << 8) | b
[3]);
522 * 'getshort()' - Get a 16-bit big-endian integer.
525 static int /* O - Short value */
526 getshort(FILE *fp
) /* I - File to read from */
528 unsigned char b
[2]; /* Bytes from file */
532 return ((b
[0] << 8) | b
[1]);
537 * 'putlong()' - Put a 32-bit big-endian integer.
540 static int /* O - 0 on success, -1 on error */
541 putlong(long n
, /* I - Long to write */
542 FILE *fp
) /* I - File to write to */
544 if (putc(n
>> 24, fp
) == EOF
)
546 if (putc(n
>> 16, fp
) == EOF
)
548 if (putc(n
>> 8, fp
) == EOF
)
550 if (putc(n
, fp
) == EOF
)
558 * 'putshort()' - Put a 16-bit big-endian integer.
561 static int /* O - 0 on success, -1 on error */
562 putshort(unsigned short n
, /* I - Short to write */
563 FILE *fp
) /* I - File to write to */
565 if (putc(n
>> 8, fp
) == EOF
)
567 if (putc(n
, fp
) == EOF
)
575 * 'read_rle8()' - Read 8-bit RLE data.
578 static int /* O - Value on success, -1 on error */
579 read_rle8(FILE *fp
, /* I - File to read from */
580 unsigned short *row
, /* O - Data */
581 int xsize
) /* I - Width of data in pixels */
583 int i
, /* Looping var */
584 ch
, /* Current character */
585 count
, /* RLE count */
586 length
; /* Number of bytes read... */
593 if ((ch
= getc(fp
)) == EOF
)
603 for (i
= 0; i
< count
; i
++, row
++, xsize
--, length
++)
610 for (i
= 0; i
< count
; i
++, row
++, xsize
--)
615 return (xsize
> 0 ? -1 : length
);
620 * 'read_rle16()' - Read 16-bit RLE data.
623 static int /* O - Value on success, -1 on error */
624 read_rle16(FILE *fp
, /* I - File to read from */
625 unsigned short *row
, /* O - Data */
626 int xsize
) /* I - Width of data in pixels */
628 int i
, /* Looping var */
629 ch
, /* Current character */
630 count
, /* RLE count */
631 length
; /* Number of bytes read... */
638 if ((ch
= getshort(fp
)) == EOF
)
648 for (i
= 0; i
< count
; i
++, row
++, xsize
--, length
++)
655 for (i
= 0; i
< count
; i
++, row
++, xsize
--)
660 return (xsize
> 0 ? -1 : length
* 2);
665 * 'write_rle8()' - Write 8-bit RLE data.
668 static int /* O - Length on success, -1 on error */
669 write_rle8(FILE *fp
, /* I - File to write to */
670 unsigned short *row
, /* I - Data */
671 int xsize
) /* I - Width of data in pixels */
673 int length
, /* Length in bytes */
674 count
, /* Number of repeating pixels */
676 x
; /* Current column */
677 unsigned short *start
, /* Start of current sequence */
678 repeat
; /* Repeated pixel */
681 for (x
= xsize
, length
= 0; x
> 0;)
687 while (x
> 0 && (row
[-2] != row
[-1] || row
[-1] != row
[0]))
699 i
= count
> 126 ? 126 : count
;
702 if (putc(128 | i
, fp
) == EOF
)
708 if (putc(*start
, fp
) == EOF
)
725 while (x
> 0 && *row
== repeat
)
734 i
= count
> 126 ? 126 : count
;
737 if (putc(i
, fp
) == EOF
)
741 if (putc(repeat
, fp
) == EOF
)
749 if (putc(0, fp
) == EOF
)
757 * 'write_rle16()' - Write 16-bit RLE data.
760 static int /* O - Length in words */
761 write_rle16(FILE *fp
, /* I - File to write to */
762 unsigned short *row
, /* I - Data */
763 int xsize
) /* I - Width of data in pixels */
765 int length
, /* Length in words */
766 count
, /* Number of repeating pixels */
768 x
; /* Current column */
769 unsigned short *start
, /* Start of current sequence */
770 repeat
; /* Repeated pixel */
773 for (x
= xsize
, length
= 0; x
> 0;)
779 while (x
> 0 && (row
[-2] != row
[-1] || row
[-1] != row
[0]))
791 i
= count
> 126 ? 126 : count
;
794 if (putshort(128 | i
, fp
) == EOF
)
800 if (putshort(*start
, fp
) == EOF
)
817 while (x
> 0 && *row
== repeat
)
826 i
= count
> 126 ? 126 : count
;
829 if (putshort(i
, fp
) == EOF
)
833 if (putshort(repeat
, fp
) == EOF
)
841 if (putshort(0, fp
) == EOF
)
849 * End of "$Id: image-sgilib.c 6649 2007-07-11 21:46:42Z mike $".