/* * * File: pict.c * Project: libpict 1.1 * Date: Fri, May 19, 1995 * (c) SPDsoft 1992-95 * */ #include #include #include #include "pict.h" #ifndef SEEK_SET #define SEEK_SET 0 #endif #if defined (_SUN_) || defined (__osf__) || defined (_MSC_VER) # define SCERO_B (char*)&cero # define SCERO_W (char*)&cero # define EndPicOp (char*)&EndOfPict #ifdef LITEND short int EndOfPict=0xff00; #else short int EndOfPict=0x00ff; #endif short int cero=0x0000; #else # define SCERO_B "" # define SCERO_W "\x00" #ifdef LITEND # define EndPicOp "\xFF\x00" #else # define EndPicOp "\x00\xFF" #endif #endif /* sun , osf*/ char sccsid[] = "@(#) libpict 1.1, (c) SPDsoft May 19 1995"; #ifdef __STDC__ Pict pict_open( char *name,int file_mode, int img_H,int img_W,int img_mode,C_MAP p_map) #else /* __STDC__ */ Pict pict_open(name,file_mode, img_H,img_W,img_mode,p_map) char *name;int file_mode, img_H,img_W,img_mode;C_MAP p_map; #endif /* __STDC__ */ { Pict pict; U_char mode; pict = (Pict)malloc(sizeof(PictStruct)); if (pict==NULL) { fprintf(stderr,"No hay sitio para un PICT.\n"); return NULL; } pict->file_mode = file_mode % 2; switch (pict->file_mode) { case P_READ: if( file_mode == P_READ ) { if ( (pict->fp=fopen(name,"rb")) == NULL ) { fprintf(stderr,"Error al abrir %s\n",name); exit(1); } } else { pict->fp = stdin; } fseek(pict->fp,(long int)(512+53),SEEK_SET); fread((void *)&mode,sizeof(U_char),1,pict->fp); rewind(pict->fp); switch(mode) { case 0x9A: pict_read_header_9A(pict); break; case 0x98: pict_read_header_98(pict,pict->map); break; default: pict_close(pict); return NULL; } break; case P_WRITE: if( file_mode == P_WRITE ) { if ( (pict->fp=fopen(name,"wb")) == NULL ) { fprintf(stderr,"Error al abrir %s\n",name); exit(1); } } else { pict->fp = stdout; } pict->clr_mode = img_mode; switch(pict->clr_mode) { case M_RGBA32: case M_RGB24: pict_write_header_9A(pict, (US_int)img_H,(US_int)img_W, (US_int)pict->clr_mode); break; case M_RGB8: memcpy((void *)pict->map, (void *)p_map,sizeof(C_MAP)); case M_MONO: pict_write_header_98(pict, (US_int)img_H,(US_int)img_W, (US_int)pict->clr_mode, pict->map); break; } break; default: fprintf(stderr,"Modo desconocido: %x\n",mode); break; } return pict; } #ifdef __STDC__ int pict_put_line(Pict pict,U_char *pixels) #else /* __STDC__ */ int pict_put_line(pict,pixels) Pict pict;U_char *pixels; #endif /* __STDC__ */ { unsigned short int byteCnt; U_char hi_bC,lo_bC; long int pos_byteCnt,pos_fin; if (pict->file_mode!=P_WRITE) return -1; /* Escribiendo Datos empaquetados por filas, fx/r/g/b(32) */ pos_byteCnt = ftell(pict->fp); byteCnt=0; fwrite((char *)&byteCnt,(size_t)pict->ctr_size,1,pict->fp); switch(pict->clr_mode) { case M_RGBA32: byteCnt += pack_bytes(pixels,pict->image_W,pict->fp); pixels += pict->image_W; case M_RGB24: byteCnt += pack_bytes(pixels,pict->image_W,pict->fp); pixels += pict->image_W; byteCnt += pack_bytes(pixels,pict->image_W,pict->fp); pixels += pict->image_W; byteCnt += pack_bytes(pixels,pict->image_W,pict->fp); pixels += pict->image_W; break; case M_RGB8: case M_MONO: byteCnt = pack_bytes(pixels,pict->image_W,pict->fp); break; } pos_fin = ftell(pict->fp); if ( (pict->clr_mode==M_RGB8) || (pict->clr_mode==M_MONO) ) { fwrite(SCERO_W,WORD,1,pict->fp); byteCnt+=2; pos_fin+=2; } fseek(pict->fp,pos_byteCnt,SEEK_SET); hi_bC = byteCnt/256; lo_bC = byteCnt%256; if (pict->ctr_size==WORD) fwrite((char *)&hi_bC,1,1,pict->fp); fwrite((char *)&lo_bC,1,1,pict->fp); fseek(pict->fp,pos_fin,SEEK_SET); return byteCnt; } #ifdef __STDC__ int pict_get_line(Pict pict,U_char *pixels) #else /* __STDC__ */ int pict_get_line(pict,pixels) Pict pict;U_char *pixels; #endif /* __STDC__ */ { unsigned short int byteCnt; U_char hi_bC,lo_bC; if (pict->file_mode!=P_READ) return -1; if (pict->ctr_size==WORD) fread((char *)&hi_bC,1,1,pict->fp); else hi_bC=0; fread((char *)&lo_bC,1,1,pict->fp); byteCnt = hi_bC*256+lo_bC; unpack_bytes(pixels,byteCnt,pict->fp) ; return pict->image_W; } #ifdef __STDC__ int pict_close(Pict pict) #else /* __STDC__ */ int pict_close(pict) Pict pict; #endif /* __STDC__ */ { int pos; switch(pict->file_mode) { case P_WRITE: pos = ftell(pict->fp); if (pos%2 != 0) fwrite(SCERO_B,1,1,pict->fp); fwrite(EndPicOp,WORD,1,pict->fp); case P_READ: if (fclose(pict->fp)==EOF) return EOF; break; } return 0; } #ifdef __STDC__ int pict_free(Pict pict) #else /* __STDC__ */ int pict_free(pict) Pict pict; #endif /* __STDC__ */ { free(pict->pixels); free(pict); return 0; } #ifdef __STDC__ int pict_height(Pict pict) #else /* __STDC__ */ int pict_height(pict) Pict pict; #endif /* __STDC__ */ { return (int)(pict->image_H); } #ifdef __STDC__ int pict_width(Pict pict) #else /* __STDC__ */ int pict_width(pict) Pict pict; #endif /* __STDC__ */ { return (int)(pict->image_W); } #ifdef __STDC__ Pict pict_read_pict(char *file) #else /* __STDC__ */ Pict pict_read_pict(file) char *file; #endif /* __STDC__ */ { Pict pict; int i,j; U_char *pix; U_char *unpacked,*r_ptr,*g_ptr,*b_ptr,*a_ptr; pict = pict_open(file,file==NULL?P_STDI:P_READ,0,0,0,NULL); pict->pixels = (U_char *)calloc( (size_t)pict->image_H * (size_t)pict->image_W , (size_t)pict->n_comp ); unpacked = (U_char *)malloc( (size_t)pict->image_W * (size_t)pict->n_comp + 1 ); /* Modif Oct 93 */ if ((pict->pixels==NULL) || ( unpacked==NULL)) { fprintf(stderr,"No memory\n"); return( (Pict)NULL ); } pix = pict->pixels; for (i=0; iimage_H; i++) { a_ptr = unpacked; r_ptr = a_ptr+pict->image_W; g_ptr = r_ptr+pict->image_W; b_ptr = g_ptr+pict->image_W; pict_get_line(pict,unpacked); switch(pict->clr_mode) { case M_MONO: case M_RGB8: for (j=0; jimage_W; j++) *pix++ = *a_ptr++; break; case M_RGB24: for (j=0; jimage_W; j++) { *pix++ = *a_ptr++; *pix++ = *r_ptr++; *pix++ = *g_ptr++; } break; case M_RGBA32: for (j=0; jimage_W; j++) { *pix++ = *r_ptr++; *pix++ = *g_ptr++; *pix++ = *b_ptr++; *pix++ = *a_ptr++; } break; } } free((void*)unpacked); /* added Apr 93 */ pict_close(pict); return pict; } #ifdef __STDC__ Pict pict_read_pict_c(char *file) #else /* __STDC__ */ Pict pict_read_pict_c(file) char *file; #endif /* __STDC__ */ { Pict pict; int i,j; U_char *unpacked; pict = pict_open(file,file==NULL?P_STDI:P_READ,0,0,0,NULL); if (pict->n_comp==1) { pict->pixels = (U_char *)malloc( (size_t)pict->image_H * (size_t)pict->image_W +1 ); } else { pict->pixels = (U_char *)calloc( (size_t)pict->image_H * (size_t)pict->image_W , (size_t)pict->n_comp ); } if (pict->pixels==NULL) { fprintf(stderr,"No memory\n"); return( (Pict)NULL ); } unpacked = pict->pixels; j=pict->image_W*pict->n_comp; for (i=0; iimage_H; i++,unpacked += j ) pict_get_line(pict,unpacked); /* Modo PICT2: linea a ( si existe ),linea r,linea g,linea b */ /* added Apr 93 */ pict_close(pict); return pict; } #ifdef __STDC__ int pict_set_map(Pict pict,C_MAP map) #else /* __STDC__ */ int pict_set_map(pict,map) Pict pict;C_MAP map; #endif /* __STDC__ */ { int i; if (pict->clr_mode!=M_MONO && pict->clr_mode!=M_RGB8) return 0; for (i=0; i<256; i++) { pict->map[i][0] = map[i][0]; pict->map[i][1] = map[i][1]; pict->map[i][2] = map[i][2]; } return 1; } #ifdef __STDC__ int pict_get_map(Pict pict,C_MAP map) #else /* __STDC__ */ int pict_get_map(pict,map) Pict pict;C_MAP map; #endif /* __STDC__ */ { int i; if (pict->clr_mode!=M_MONO && pict->clr_mode!=M_RGB8) return 0; for (i=0; i<256; i++) { map[i][0] = pict->map[i][0]; map[i][1] = pict->map[i][1]; map[i][2] = pict->map[i][2]; } return 1; } #ifdef __STDC__ U_char *pict_pixels(Pict pict) #else /* __STDC__ */ U_char *pict_pixels(pict) Pict pict; #endif /* __STDC__ */ { return pict->pixels; } #ifdef __STDC__ int pict_mode(Pict pict) #else /* __STDC__ */ int pict_mode(pict) Pict pict; #endif /* __STDC__ */ { return (int)(pict->clr_mode); } #ifdef __STDC__ int pict_n_comp(Pict pict) #else /* __STDC__ */ int pict_n_comp(pict) Pict pict; #endif /* __STDC__ */ { return (int)(pict->n_comp); } #ifdef __STDC__ void pict_pixel_rgba( Pict pict, int i, int j, U_char *r, U_char *g, U_char *b, U_char *a ) #else /* __STDC__ */ void pict_pixel_rgba(pict,i,j,r,g,b,a) Pict pict; int i,j; U_char *r, *g, *b, *a; #endif /* __STDC__ */ { U_char *pixel; U_char rr,gg,bb,aa; switch(pict->clr_mode) { case M_MONO: pixel = pict->pixels + (long int)i*(long int)pict->image_W + (long int)j; rr = gg = bb = pict->map[*pixel][0]; aa = 0; break; case M_RGB8: pixel = pict->pixels + (long int)i*(long int)pict->image_W + (long int)j; rr = pict->map[*pixel][0]; gg = pict->map[*pixel][1]; bb = pict->map[*pixel][2]; aa = 0; break; case M_RGB24: pixel = pict->pixels + 3*((long int)i*(long int)pict->image_W + (long int)j); rr = pixel[0]; gg = pixel[1]; bb = pixel[2]; aa = 0; break; case M_RGBA32: pixel = pict->pixels + 4*((long int)i*(long int)pict->image_W + (long int)j); rr = pixel[0]; gg = pixel[1]; bb = pixel[2]; aa = pixel[3]; break; default: rr = gg = bb = aa = 0; break; } if (r != NULL) *r = rr; if (g != NULL) *g = gg; if (b != NULL) *b = bb; if (a != NULL) *a = aa; }