/****************************************************************************** ESE 558 Digital Image Processing rasio.c This file contains routines to handle the input and output of sun rasterfile images. ******************************************************************************/ #include #include #include #include "rasio.h" int swabb; /****************************************************************************** read_raster_file Reads in input raster file. Uses pic_alloc to allocate space for the image. ******************************************************************************/ Raster_file *read_raster_file( char *infilename) { int count,i; Raster_file *raster_file; Raster_header *raster_header; unsigned char **image; char buf[100]; int width, height; unsigned char color_map[768]; FILE *fp; if((fp = fopen(infilename, "rb")) == NULL){ fprintf(stderr, "File open error!\n"); exit(0); } raster_file = (Raster_file*) malloc(sizeof(Raster_file)); if( !raster_file) { fprintf(stderr,"memory allocation error raster file\n"); exit(1); } raster_header = (Raster_header*) malloc(sizeof(Raster_header)); if( !raster_header) { fprintf(stderr,"memory allocation error raster header \n"); exit(1); } /* read header */ if((count = fread( raster_header, sizeof(Raster_header), 1, fp)) != 1){ sprintf(buf, "count = %d expected = %d\n", count, 1); fprintf(stderr, buf); exit(0); } swabb = 0; /* verify header information */ if(raster_header->magic != RAS_MAGIC){ swap_header( raster_header); swabb = 1; if(raster_header->magic != RAS_MAGIC){ sprintf(buf, "Input error. Image format is not sun raster\n"); fprintf(stderr, buf); exit(1); } } height = raster_header->height; width = raster_header->width; if((height<=0) || (height>1024*1024)){ sprintf(buf, "image height should be <= 1024*1024 and >= 0\n"); fprintf(stderr, buf); exit(1); } if((width <= 0) || (width>1024*1024)) { sprintf(buf, "width should be <= 1024*1024 and >= 0\n"); fprintf(stderr, buf); exit(1); } if ((raster_header->depth != 8) || /* depth (1, 8, or 24 bits) of pixel */ (raster_header->length != height * width) || /* length (bytes) of image */ (raster_header->type != RT_STANDARD)) /* type of file; see RT_* below */ { sprintf(buf, "Alert! Input image header format error.\n"); fprintf(stderr, buf); exit(1); } /* read colormap if one exists */ /* raster_header-> maptype = RMT_NONE; */ /* type of colormap; see RMT_* below */ /* raster_header-> maplength = 0; */ /* length (bytes) of following map */ if(raster_header->maptype != RMT_NONE){ if((count = fread( color_map, sizeof(unsigned char), raster_header->maplength, fp)) != raster_header->maplength) { sprintf(buf, "count = %d expected = %d\n", count, raster_header->maplength); fprintf(stderr, buf); exit(0); } } image = pic_alloc( height, width); /* read image in */ for(i=0;iimage = image; raster_file->header = raster_header; return( raster_file); } /* end read_raster_file */ /****************************************************************************** write_raster_file ******************************************************************************/ void write_raster_file(char *outfilename,unsigned char **image,int height, int width) { Raster_header raster_header; unsigned char red[256], green[256], blue[256]; char buf[100]; int i, count; FILE *fp; if((fp = fopen(outfilename, "wb")) == NULL){ sprintf(buf, "File open error!\n"); fprintf(stderr, buf); exit(0); } raster_header.magic = RAS_MAGIC; raster_header.height = height; raster_header.width = width%2 == 0 ? width : width +1; if(raster_header.width > width){ fprintf(stderr, "Width increments to be even in the converted rasterfile!"); } raster_header.length = raster_header.width * raster_header.height; raster_header.depth = 8; raster_header.type = RT_STANDARD; raster_header.maptype = RMT_EQUAL_RGB; /* 1 */ raster_header.maplength = 256*3; /* always generate colormap of 256 gray-levels */ if (swabb) swap_header( &raster_header); /* create gray color map */ for(i=0;i<256;i++){ red[i] = (unsigned char)i; green[i] = (unsigned char)i; blue[i] = (unsigned char)i; } /* write header */ if((count = fwrite( &raster_header, sizeof(Raster_header), 1, fp)) != 1){ sprintf(buf, "count = %d expected = %d", count, 1); fprintf(stderr, buf); exit(0); } /* write colormap */ if((count = fwrite( red, sizeof(unsigned char), 256, fp)) != 256){ sprintf(buf, "count = %d expected = %d", count, 256); fprintf(stderr, buf); exit(0); } if((count = fwrite( green, sizeof(unsigned char), 256, fp)) != 256){ sprintf(buf, "count = %d expected = %d", count, 256); fprintf(stderr, buf); exit(0); } if((count = fwrite( blue, sizeof(unsigned char), 256, fp)) != 256){ sprintf(buf, "count = %d expected = %d", count, 256); fprintf(stderr, buf); exit(0); } /* write Image data */ for(i=0;i stream int 1 2 3 4 <---- 1 is highest, 4 is lowest left << >> right (the direction of the shift operator) pc: BYTE 1 2 3 4 ----> stream int 4 3 2 1 <----- 1 is lowest, 4 is highest left << >> right (the direction of the shift operator) ******************************************************************************/ void swap_int(int *input){ unsigned char byte3, byte2, byte1; byte3 = HIBYTE(LOWORD(*input)); byte2 = LOBYTE(HIWORD(*input)); byte1 = HIBYTE(HIWORD(*input)); *input = (*input << 8) | (int) byte3; *input = (*input << 8) | (int) byte2; *input = (*input << 8) | (int) byte1; } /****************************************************************************** swap_header swaps all the variables in the header ******************************************************************************/ void swap_header( Raster_header *header) { swap_int(&header->magic); swap_int(&header->width); swap_int(&header->height); swap_int(&header->depth); swap_int(&header->length); swap_int(&header->type); swap_int(&header->maptype); swap_int(&header->maplength); }