home *** CD-ROM | disk | FTP | other *** search
- /*
- * rdsprite.c
- * © Dave Thomas, Tue 10th October 1995
- *
- * This file contains routines to read RISC OS Sprite images.
- */
-
- #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
- #include "DeskLib:h.KernelSWIs" /* swi i/f */
-
- #ifdef SPRITE_SUPPORTED
-
-
- /* Macros to deal with unsigned chars as efficiently as compiler allows */
-
- #ifdef HAVE_UNSIGNED_CHAR
- typedef unsigned char U_CHAR;
- #define UCH(x) ((int) (x))
- #else /* !HAVE_UNSIGNED_CHAR */
- #ifdef CHAR_IS_UNSIGNED
- typedef char U_CHAR;
- #define UCH(x) ((int) (x))
- #else
- typedef char U_CHAR;
- #define UCH(x) ((int) (x) & 0xFF)
- #endif
- #endif /* HAVE_UNSIGNED_CHAR */
-
-
- #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
-
-
- /* Private version of data source object */
-
- typedef struct _sprite_source_struct * sprite_source_ptr;
-
- typedef struct _sprite_source_struct {
- struct cjpeg_source_struct pub; /* public fields */
-
- j_compress_ptr cinfo; /* back link saves passing separate parm */
-
- JSAMPARRAY colormap; /* Sprite colourmap (converted to my format) */
-
- jvirt_sarray_ptr whole_image; /* Needed to convert 32bpp images down */
- JDIMENSION source_row; /* Current source row number */
- JDIMENSION row_width; /* Physical width of scanlines in file */
-
- int bits_per_pixel; /* remembers 8- or 24-bit format */
- } sprite_source_struct;
-
-
- LOCAL int
- read_byte (sprite_source_ptr sinfo)
- /* Read next byte from sprite file */
- {
- register FILE *infile = sinfo->pub.input_file;
- register int c;
-
- if ((c = getc(infile)) == EOF)
- ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
- return c;
- }
-
-
- LOCAL void
- read_colormap (sprite_source_ptr sinfo, int cmaplen, int mapentrysize)
- /* Read the colourmap from a sprite file */
- {
- int i;
-
- if (mapentrysize != 4)
- ERREXIT(sinfo->cinfo, JERR_SPRITE_BADCMP);
-
- /* &BbGgRrAa,&BbGgRrAa format (flash on, flash off) */
- for (i = 0; i < cmaplen; i++) {
- (void) read_byte(sinfo);
- sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
- sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
- sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
- (void) read_byte(sinfo);
- (void) read_byte(sinfo);
- (void) read_byte(sinfo);
- (void) read_byte(sinfo);
- }
- break;
- }
-
-
- /*
- * Read one row of pixels.
- * The image has been read into the whole_image array, but is otherwise
- * unprocessed. We must read it out in top-to-bottom row order, and if
- * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
- */
-
- METHODDEF JDIMENSION
- get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- /* This version is for reading 8-bit colormap indexes */
- {
- sprite_source_ptr source = (sprite_source_ptr) sinfo;
- register JSAMPARRAY colormap = source->colormap;
- JSAMPARRAY image_ptr;
- register int p;
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
-
- /* Expand the colormap indexes to real data */
- inptr = image_ptr[0];
- outptr = source->pub.buffer[0];
- for (col = cinfo->image_width; col > 0; col--) {
- p = GETJSAMPLE(*inptr++);
- *outptr++ = colormap[0][p];
- *outptr++ = colormap[1][p];
- *outptr++ = colormap[2][p];
- }
-
- return 1;
- }
-
-
- METHODDEF JDIMENSION
- get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- }
-
- METHODDEF JDIMENSION
- get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- sprite_source_ptr source = (sprite_source_ptr) sinfo;
- JSAMPARRAY image_ptr;
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
-
- /* Fetch next row from virtual array */
- source->source_row--;
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, source->whole_image,
- source->source_row, (JDIMENSION) 1, FALSE);
-
- inptr = image_ptr[0];
- outptr = source->pub.buffer[0];
- for (col = cinfo->image_width; col > 0; col--) {
- (void) = *inptr++;
- outptr[0] = *inptr++;
- outptr[1] = *inptr++;
- outptr[2] = *inptr++;
- outptr += 3;
- }
-
- return 1;
- }
-
-
- /*
- * This method loads the image into whole_image during the first call on
- * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
- * get_8bit_row or get_24bit_row on subsequent calls.
- */
-
- METHODDEF JDIMENSION
- preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- sprite_source_ptr source = (sprite_source_ptr) sinfo;
- register FILE *infile = source->pub.input_file;
- register int c;
- register JSAMPROW out_ptr;
- JSAMPARRAY image_ptr;
- JDIMENSION row, col;
- cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
-
- /* Read the data into a virtual array in input-file row order. */
- for (row = 0; row < cinfo->image_height; row++) {
- if (progress != NULL) {
- progress->pub.pass_counter = (long) row;
- progress->pub.pass_limit = (long) cinfo->image_height;
- (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
- }
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, source->whole_image,
- row, (JDIMENSION) 1, TRUE);
- out_ptr = image_ptr[0];
- for (col = source->row_width; col > 0; col--) {
- /* inline copy of read_byte() for speed */
- if ((c = getc(infile)) == EOF)
- ERREXIT(cinfo, JERR_INPUT_EOF);
- *out_ptr++ = (JSAMPLE) c;
- }
- }
- if (progress != NULL)
- progress->completed_extra_passes++;
-
- /* Set up to read from the virtual array in top-to-bottom order */
- switch (source->bits_per_pixel) {
- case 8:
- source->pub.get_pixel_rows = get_8bit_row;
- break;
- case 24:
- source->pub.get_pixel_rows = get_24bit_row;
- break;
- default:
- ERREXIT(cinfo, JERR_sprite_BADDEPTH);
- }
- source->source_row = cinfo->image_height;
-
- /* And read the first row */
- return (*source->pub.get_pixel_rows) (cinfo, sinfo);
- }
-
-
- /*
- * Read the file header; return image size and component count.
- */
-
- #define WORD(array,offset) ((INT32) UCH(array[offset]) + \
- (((INT32) UCH(array[offset+1])) << 8) + \
- (((INT32) UCH(array[offset+2])) << 16) + \
- (((INT32) UCH(array[offset+3])) << 24))
-
- METHODDEF void
- start_input_sprite (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- sprite_source_ptr source = (sprite_source_ptr) sinfo;
- U_CHAR spriteheader[56];
- INT32 noofsprites;
- INT32 offsettofirst;
- INT32 offsettofree;
- INT32 offsettonext;
- U_CHAR spritename[12];
- INT32 width;
- INT32 height;
- INT32 firstbit;
- INT32 lastbit;
- INT32 offsettosprite;
- INT32 offsettomask;
- INT32 mode;
- INT32 bpp, colours, xdpi, ydpi;
-
- width = WORD(spriteinfoheader,24) + 1;
- height = WORD(spriteinfoheader,28) + 1;
- firstbit = WORD(spriteinfoheader,32);
- lastbit = WORD(spriteinfoheader,36);
- offsettosprite = WORD(spriteinfoheader,40);
- mode = WORD(spriteinfoheader,48);
-
- OS_ReadModeVariable(mode, 9, &bpp);
- bpp = 1<<bpp;
- colours = 1<<bpp;
- width = (width*32-(31-lastbit)-firstbit)/bpp
-
- source->bits_per_pixel = bpp;
-
- switch (bpp) {
- case 8:
- mapentrysize = 4;
- TRACEMS2(cinfo, 1, JTRC_SPRITE_MAPPED, (int) width, (int) height);
- break;
- case 16, 32:
- TRACEMS2(cinfo, 1, JTRC_SPRITE, (int) width, (int) height);
- break;
- default:
- ERREXIT(cinfo, JERR_SPRITE_BADDEPTH);
- break;
- }
-
- /* Set JFIF density parameters from the sprite data */
- OS_ReadModeVariable(mode, 4, &xdpi);
- OS_ReadModeVariable(mode, 5, &ydpi);
- cinfo->X_density = (UINT16) (180/xdpi);
- cinfo->Y_density = (UINT16) (180/ydpi);
- cinfo->density_unit = 1; /* dots/inch **CHECK THIS!!** */
-
- /* Read the colormap, if any */
- if (mapentrysize > 0) {
- /* Allocate space to store the colormap */
- source->colormap = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) colours, (JDIMENSION) 3);
- /* and read it from the file */
- read_colormap(source, (int) colours, mapentrysize);
- }
-
- /* Compute row width in file */
- source->row_width = (JDIMENSION) (WORD(spriteinfoheader,24) + 1) * 4;
-
- /* Allocate one-row buffer for returned data */
- source->pub.buffer = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (JDIMENSION) (width * 3), (JDIMENSION) 1);
- source->pub.buffer_height = 1;
-
- cinfo->in_color_space = JCS_RGB;
- cinfo->input_components = 3;
- cinfo->data_precision = 8;
- cinfo->image_width = (JDIMENSION) width;
- cinfo->image_height = (JDIMENSION) height;
- }
-
-
- /*
- * Finish up at the end of the file.
- */
-
- METHODDEF void
- finish_input_sprite (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- /* no work */
- }
-
-
- /*
- * The module selection routine for sprite format input.
- */
-
- GLOBAL cjpeg_source_ptr
- jinit_read_sprite (j_compress_ptr cinfo)
- {
- sprite_source_ptr source;
-
- /* Create module interface object */
- source = (sprite_source_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(sprite_source_struct));
- source->cinfo = cinfo; /* make back link for subroutines */
- /* Fill in method ptrs, except get_pixel_rows which start_input sets */
- source->pub.start_input = start_input_sprite;
- source->pub.finish_input = finish_input_sprite;
-
- return (cjpeg_source_ptr) source;
- }
-
- #endif /* sprite_SUPPORTED */
-