/* ********************************************************************* file rff.c: Rff image manipulation routines. This file contains a set of routines for manipulating standard rasterfile format images. to compile to an object file: cc -c rff.c then when linking your programs, make sure to link with the pixrect library cc test.o rff.o -o test -lpxirect ... ********************************************************************** */ # include # include #define FALSE 0 #define TRUE 1 /* This procedure reads in an image from the open file fp, and returns a pointer to the pixrect which now holds the image. Memory is allocated for the colormap if the image is 8 bit, and the colormap is loaded and assigned to the pixrect. This procedure will work for any depth of image. */ struct pixrect *read_image(fp,colormap) FILE *fp; colormap_t *colormap; { struct rasterfile temp; struct pixrect *screen_temp,*pr_load_image(); pr_load_header(fp,&temp); colormap->type=temp.ras_maptype; colormap->length=temp.ras_maplength/3; colormap->map[0]=(unsigned char *)malloc (colormap->length*sizeof(unsigned char)); colormap->map[1]=(unsigned char *)malloc (colormap->length*sizeof(unsigned char)); colormap->map[2]=(unsigned char *)malloc (colormap->length*sizeof(unsigned char)); if (colormap->map[2]==NULL) { /* if (malloc_verify()==1) exerror("insufficient memory but verify passed\n"); else */ exerror("insufficient memory and verify failed\n"); } /* exerror("rff: Insufficient memory to run\n"); */ pr_load_colormap(fp, &temp, colormap); screen_temp=pr_load_image(fp, &temp, colormap); pr_putcolormap(screen_temp,0,256,colormap->map[0],colormap->map[1],colormap->map[2]); if (screen_temp == NULL) exerror ("rff: Unable to load image\n"); return(screen_temp); } /* load_rff_image copies the pixel data from the pixrect pointed to by screen into the array pointed to by image. Memory must be allocated for the array before the function is called. The image can be either 8 bit or 1 bit. */ load_rff_image (screen,image) struct pixrect *screen; long *image; { switch(screen->pr_depth){ case 1: load_1bit_image(screen,image); break; case 8: load_8bit_image(screen,image); break; default: exerror("load_rff_image: %d bit pixels not supported\n",screen->pr_depth); break; } } load_1bit_image (screen,image) struct pixrect *screen; long *image; { struct mpr_data *temp; unsigned char *arr,byte; int i,j,x,dx,dy,num_bytes; dx = screen->pr_size.x; dy = screen->pr_size.y; temp = mpr_d(screen); arr = (unsigned char *)temp->md_image; for (i=0;ipr_size.y;i++){ for (j=0;j> 7; byte = byte << 1; ++image; } } if (dx % 8) { byte = *arr; for (x=0;x> 7; byte = byte << 1; ++image; } ++arr; } num_bytes = dx /8; if (dx % 8) ++num_bytes; if (num_bytes % 2) ++arr; } } load_8bit_image (screen,image) struct pixrect *screen; long *image; { struct mpr_data *temp; unsigned char *arr; int i,j,dx,dy; dx = screen->pr_size.x; dy = screen->pr_size.y; temp = mpr_d(screen); arr = (unsigned char *)temp->md_image; for (i=0;ipr_size.y;i++){ for (j=0;jpr_size.x;j++){ *image = (long)*arr; ++image; ++arr; } if (screen->pr_size.x % 2 != 0) ++arr; } } /* dump rff image copies pixel data from the array pointed to by image into the pixrect pointed to by screen. The pixrect pointed to by screen must be allocated and have its size and depth set, such as one created by a call to mem_create() {see pixrect manual}. The pixrect can be either 1 or 8 bit. */ dump_rff_image(screen,image) struct pixrect *screen; long *image; { switch(screen->pr_depth){ case 1: dump_1bit_image(screen,image); break; case 8: dump_8bit_image(screen,image); break; default: exerror("dump_rff_image: %d bit pixels not supported\n", screen->pr_depth); break; } } dump_1bit_image(screen,image) struct pixrect *screen; long *image; { struct mpr_data *temp; unsigned char *arr,byte; int i,j,x,dx,dy,num_bytes; dx = screen->pr_size.x; dy = screen->pr_size.y; temp = mpr_d(screen); arr = (unsigned char *)temp->md_image; for (i=0;ipr_size.y;i++){ for (j=0;jpr_size.x/8;j++){ byte = 0; for (x=0;x<8;x++){ byte = (byte << 1) + *image; image++; } *arr = byte; ++arr; } if (dx % 8) { byte = 0; for (x=0;xpr_size.x; dy = screen->pr_size.y; temp = mpr_d(screen); arr = (unsigned char *)temp->md_image; for (i=0;ipr_size.y;i++){ for (j=0;jpr_size.x;j++){ *arr = (char)*image; ++image; ++arr; } if (screen->pr_size.x % 2 != 0) ++arr; } } /* print error message and exit abnormally. */ exerror (msg, par1, par2, par3) char * msg; { fprintf (stderr, msg, par1, par2, par3); exit (1); }