unsigned short imb_update_iff(int file, int code) { int buf[2], filelen, skip; uchar nop; if (file<=0) return (FALSE); filelen = BLI_filesize(file)-8; /* calc filelength */ lseek(file,0L,2); /* seek end */ if (filelen & 1){ /* make length 'even' */ switch(code){ case BODY: nop = IFFNOP; break; } if (write(file,&nop,1)!=1) return (FALSE); filelen++; } lseek(file,4L,0); buf[0] = BIG_LONG(filelen); if (write(file, buf, 4) != 4) return (FALSE); if (code == 0) return (TRUE); filelen-=4; lseek(file,4L,1); while (filelen>0){ /* seek BODY */ read(file, buf, 8); filelen -= 8; if (buf[0] == code) break; skip = (BIG_LONG(buf[1]) + 1) & ~1; filelen -= skip; lseek(file, skip, 1); } if (filelen <= 0) { printf("update_iff: couldn't find chunk\n"); return (FALSE); } lseek(file, -4L, 1); buf[0] = BIG_LONG(filelen); if (write(file, buf, 4)!=4) return (FALSE); return (TRUE); }
static int IMB_ispic_name(const char *name) { ImFileType *type; struct stat st; int fp, buf[10]; if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name); if(stat(name,&st) == -1) return FALSE; if(((st.st_mode) & S_IFMT) != S_IFREG) return FALSE; if((fp = open(name,O_BINARY|O_RDONLY)) < 0) return FALSE; if(read(fp, buf, 32) != 32) { close(fp); return FALSE; } close(fp); /* XXX move this exception */ if((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return JPG; for(type=IMB_FILE_TYPES; type->is_a; type++) if(type->is_a((uchar*)buf)) return type->filetype; return FALSE; }
int IMB_ispic_type(const char *name) { /* increased from 32 to 64 because of the bitmaps header size */ #define HEADER_SIZE 64 unsigned char buf[HEADER_SIZE]; const ImFileType *type; BLI_stat_t st; int fp; BLI_assert(!BLI_path_is_rel(name)); if (UTIL_DEBUG) printf("%s: loading %s\n", __func__, name); if (BLI_stat(name, &st) == -1) return false; if (((st.st_mode) & S_IFMT) != S_IFREG) return false; if ((fp = BLI_open(name, O_BINARY | O_RDONLY, 0)) == -1) return false; memset(buf, 0, sizeof(buf)); if (read(fp, buf, HEADER_SIZE) <= 0) { close(fp); return false; } close(fp); /* XXX move this exception */ if ((BIG_LONG(((int *)buf)[0]) & 0xfffffff0) == 0xffd8ffe0) return IMB_FTYPE_JPG; for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) { if (type->is_a) { if (type->is_a(buf)) { return type->filetype; } } else if (type->is_a_filepath) { if (type->is_a_filepath(name)) { return type->filetype; } } } return 0; #undef HEADER_SIZE }
struct ImBuf *IMB_loadiffmem(int *mem, int flags) { int len,maxlen; struct ImBuf *ibuf; // IMB_loadiffmem shouldn't be used anymore in new development // it's still here to be backwards compatible... maxlen= (GET_BIG_LONG(mem+1) + 1) & ~1; if (GET_ID(mem) == CAT) { mem += 3; maxlen -= 4; while(maxlen > 0) { if (GET_ID(mem) == FORM) { len = ((GET_BIG_LONG(mem+1) + 1) & ~1) + 8; if ((GET_ID(mem+2) == ILBM) || (GET_ID(mem+2) == ANIM)) break; mem = (int *)((uchar *)mem +len); maxlen -= len; } else return(0); } } if (maxlen > 0) { if (GET_ID(mem) == FORM) { if (GET_ID(mem+2) == ILBM) { return (imb_loadamiga(mem, flags)); } else if (GET_ID(mem+5) == ILBM) { /* animaties */ return (imb_loadamiga(mem+3, flags)); } else if (GET_ID(mem+2) == ANIM) { return (imb_loadanim(mem, flags)); } } else if ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)) { return (imb_loadiris((uchar *) mem,flags)); } else if ((BIG_LONG(mem[0]) & 0xfffffff0) == 0xffd8ffe0) { return (0); } } ibuf = imb_loadtarga((uchar *) mem,maxlen,flags); if (ibuf) return(ibuf); if (IB_verbose) fprintf(stderr,"Unknown fileformat\n"); return (0); }
static boolean handle_app1 (j_decompress_ptr cinfo) { INT32 length, i; char neogeo[128]; INPUT_VARS(cinfo); length = 0; INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; if (length < 16) { for (i = 0; i < length; i++) INPUT_BYTE(cinfo, neogeo[i], return FALSE); length = 0; if (strncmp(neogeo, "NeoGeo", 6) == 0) memcpy(&ibuf_ftype, neogeo + 6, 4); ibuf_ftype = BIG_LONG(ibuf_ftype); } INPUT_SYNC(cinfo); /* do before skip_input_data */ if (length > 0) (*cinfo->src->skip_input_data) (cinfo, length); return TRUE; }
struct ImBuf *IMB_loadiffname(const char *naam, int flags) { int file; struct ImBuf *ibuf; int buf[1]; file = open(naam, O_BINARY|O_RDONLY); if (file == -1) return (0); ibuf= IMB_loadifffile(file, flags); if (ibuf == NULL) { if (read(file, buf, 4) != 4) buf[0] = 0; if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) ibuf = imb_ibJpegImageFromFilename(naam, flags); } if (ibuf) { strncpy(ibuf->name, naam, sizeof(ibuf->name)); if (flags & IB_fields) IMB_de_interlace(ibuf); } close(file); return(ibuf); }
ImBuf *imb_loadtarga(unsigned char *mem, size_t mem_size, int flags, char colorspace[IM_MAX_SPACE]) { TARGA tga; struct ImBuf *ibuf; int col, count, size; unsigned int *rect, *cmap = NULL /*, mincol = 0*/, maxcol = 0; uchar *cp = (uchar *) &col; if (checktarga(&tga, mem) == 0) return(NULL); colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); if (flags & IB_test) ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, tga.pixsize, 0); else ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, (tga.pixsize + 0x7) & ~0x7, IB_rect); if (ibuf == NULL) return(NULL); ibuf->ftype = TGA; mem = mem + 18 + tga.numid; cp[0] = 0xff; cp[1] = cp[2] = 0; if (tga.mapsize) { /* load color map */ /*mincol = tga.maporig;*/ /*UNUSED*/ maxcol = tga.mapsize; cmap = MEM_callocN(sizeof(unsigned int) * maxcol, "targa cmap"); for (count = 0; count < maxcol; count++) { switch (tga.mapbits >> 3) { case 4: cp[0] = mem[3]; cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 4; break; case 3: cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 3; break; case 2: cp[1] = mem[1]; cp[0] = mem[0]; mem += 2; break; case 1: col = *mem++; break; } cmap[count] = col; } size = 0; for (col = maxcol - 1; col > 0; col >>= 1) size++; ibuf->planes = size; if (tga.mapbits != 32) { /* set alpha bits */ cmap[0] &= BIG_LONG(0x00ffffffl); } }
ImBuf *imb_loadtarga(const unsigned char *mem, size_t mem_size, int flags, char colorspace[IM_MAX_SPACE]) { TARGA tga; struct ImBuf *ibuf; int count, size; unsigned int *rect, *cmap = NULL /*, mincol = 0*/, cmap_max = 0; int32_t cp_data; uchar *cp = (uchar *)&cp_data; if (checktarga(&tga, mem) == 0) { return NULL; } colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); if (flags & IB_test) { ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, tga.pixsize, 0); } else { ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, (tga.pixsize + 0x7) & ~0x7, IB_rect); } if (ibuf == NULL) { return NULL; } ibuf->ftype = IMB_FTYPE_TGA; if (tga.imgtyp < 4) { ibuf->foptions.flag |= RAWTGA; } mem = mem + 18 + tga.numid; cp[0] = 0xff; cp[1] = cp[2] = 0; if (tga.mapsize) { /* load color map */ /*mincol = tga.maporig;*/ /*UNUSED*/ cmap_max = tga.mapsize; cmap = MEM_callocN(sizeof(unsigned int) * cmap_max, "targa cmap"); for (count = 0; count < cmap_max; count++) { switch (tga.mapbits >> 3) { case 4: cp[0] = mem[3]; cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 4; break; case 3: cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 3; break; case 2: cp[1] = mem[1]; cp[0] = mem[0]; mem += 2; break; case 1: cp_data = *mem++; break; } cmap[count] = cp_data; } size = 0; for (int cmap_index = cmap_max - 1; cmap_index > 0; cmap_index >>= 1) { size++; } ibuf->planes = size; if (tga.mapbits != 32) { /* set alpha bits */ cmap[0] &= BIG_LONG(0x00ffffffl); } }
unsigned short imb_start_iff(struct ImBuf *ibuf, int file) { unsigned int *point, size, *buf; if ((point=buf=(unsigned int *)malloc(32768))==0) return FALSE; *point++ = FORM; /* FORMxxxxILBM in buffer */ *point++ = 0; if (IS_amiga(ibuf)){ struct BitMapHeader *bmhd; *point++ = ILBM; *point++ = CAMG; *point++ = 4; *point++ = (ibuf->ftype & 0xffff); *point++=BMHD; *point++=sizeof(struct BitMapHeader); bmhd=(struct BitMapHeader *)point; /* bmhd points to location where bmhd will be */ point=(unsigned int *)((char *)point+sizeof(struct BitMapHeader)); /* advance pointer already */ bmhd->w=ibuf->x; bmhd->h=ibuf->y; bmhd->pageWidth=ibuf->x; bmhd->pageHeight=ibuf->y; bmhd->x=0; bmhd->y=0; bmhd->nPlanes=ibuf->depth; bmhd->masking=0; if (ibuf->flags & IB_vert){ bmhd->compression=2; } else{ bmhd->compression=1; } bmhd->pad1=0; bmhd->transparentColor=0; bmhd->xAspect=1; bmhd->yAspect=1; } else if (IS_anim(ibuf)){ struct Adat *adat; extern float adat_gamma; extern float adat_distort; *point++ = ANIM; *point++ = ADAT; *point++ = BIG_LONG(sizeof(struct Adat)); adat = (struct Adat *)point; point = (unsigned int *)((char *)point+sizeof(struct Adat)); /* advance pointer already */ adat->w = BIG_SHORT(ibuf->x); adat->h = BIG_SHORT(ibuf->y); adat->type = BIG_SHORT(ibuf->ftype); adat->xorig = BIG_SHORT(ibuf->xorig); adat->yorig = BIG_SHORT(ibuf->yorig); adat->pad = 0; adat->gamma = adat_gamma; adat->distort = adat_distort; } size=((uchar *)point-(uchar *)buf); if (write(file,buf,size)!=size){ free(buf); return (FALSE); } if (ibuf->cmap){ if (IS_anim(ibuf)){ size = ibuf->maxcol * sizeof(int); buf[0] = CMAP; buf[1] = BIG_LONG(size); if (write(file,buf,8) != 8){ free(buf); return (FALSE); } if (write(file,ibuf->cmap,size) != size){ free(buf); return (FALSE); } } else{ uchar *cpoint,*cols; unsigned int i,bits; point = buf; if (IS_amiga(ibuf)){ *(point++) = CMAP; *(point++) = BIG_LONG(3*ibuf->maxcol); } cpoint = (uchar *) point; cols = (uchar *)ibuf->cmap; if ((ibuf->cbits > 0) && (ibuf->cbits < 8)){ bits = ~((1 << (8-ibuf->cbits)) - 1); } else bits = -1; if (IS_ham(ibuf)) bits = -1; for (i=0 ; i<ibuf->maxcol ; i++){ *(cpoint++) = cols[0] & bits; *(cpoint++) = cols[1] & bits; *(cpoint++) = cols[2] & bits; cols += 4; } if (ibuf->maxcol & 1) *(cpoint++)=0; size=(cpoint-(uchar *)buf); if (write(file,buf,size)!=size){ free(buf); return (FALSE); } } } if (IS_amiga(ibuf)) buf[0] = BODY; if (IS_anim(ibuf)) buf[0] = BODY; buf[1]=0; if (write(file,buf,8)!=8){ free(buf); return(FALSE); } free(buf); return (TRUE); }
static void write_jpeg(struct jpeg_compress_struct * cinfo, struct ImBuf * ibuf) { JSAMPLE * buffer = NULL; JSAMPROW row_pointer[1]; uchar * rect; int x, y; char neogeo[128]; ImMetaData *iptr; char *text; jpeg_start_compress(cinfo, TRUE); strcpy(neogeo, "NeoGeo"); ibuf_ftype = BIG_LONG(ibuf->ftype); memcpy(neogeo + 6, &ibuf_ftype, 4); jpeg_write_marker(cinfo, 0xe1, (JOCTET*) neogeo, 10); if(ibuf->metadata) { /* key + max value + "Blender" */ text= MEM_mallocN(530, "stamp info read"); iptr= ibuf->metadata; while(iptr) { if (!strcmp (iptr->key, "None")) { jpeg_write_marker(cinfo, JPEG_COM, (JOCTET *) iptr->value, strlen (iptr->value) + 1); goto next_stamp_info; } /* * The JPEG format don't support a pair "key/value" * like PNG, so we "encode" the stamp in a * single string: * "Blender:key:value" * * The first "Blender" is a simple identify to help * in the read process. */ sprintf (text, "Blender:%s:%s", iptr->key, iptr->value); jpeg_write_marker(cinfo, JPEG_COM, (JOCTET *) text, strlen (text)+1); next_stamp_info: iptr = iptr->next; } MEM_freeN(text); } row_pointer[0] = MEM_mallocN(sizeof(JSAMPLE) * cinfo->input_components * cinfo->image_width, "jpeg row_pointer"); for(y = ibuf->y - 1; y >= 0; y--) { rect = (uchar *) (ibuf->rect + y * ibuf->x); buffer = row_pointer[0]; switch(cinfo->in_color_space) { case JCS_RGB: for (x = 0; x < ibuf->x; x++) { *buffer++ = rect[0]; *buffer++ = rect[1]; *buffer++ = rect[2]; rect += 4; } break; case JCS_GRAYSCALE: for (x = 0; x < ibuf->x; x++) { *buffer++ = rect[0]; rect += 4; } break; case JCS_UNKNOWN: memcpy(buffer, rect, 4 * ibuf->x); break; /* default was missing... intentional ? */ default: ; /* do nothing */ } jpeg_write_scanlines(cinfo, row_pointer, 1); } jpeg_finish_compress(cinfo); MEM_freeN(row_pointer[0]); }
static void bptolscanl(unsigned int *buf, int size, unsigned int **list, int nobp, int offset) { /* converts bitplanes to a buffer with ints by 4 dividiable amount of bitplanes, the width of bitplanes is rounded at ints */ list += nobp; for (;nobp>0;) { int todo,i; register int bp1, bp2, bp3, bp4, data; register unsigned int *point; int loffset; /*register unsigned int bp1, bp2, bp3, bp4;*/ bp1 = bp2 = bp3 = bp4 = todo = 0; point = buf; loffset = offset; if (nobp & 1){ list -= 1; nobp -= 1; for(i=size;i>0;i--) { if (todo==0) { bp1 = BIG_LONG((list[0])[loffset]); loffset++; todo=32; } data = *point; data<<=1; if (bp1<0) data+=1; bp1<<=1; /* data += (bp1 >> 31); bp1 <<= 1; */ *(point++)=data; todo--; } } else if (nobp & 2){ list -= 2; nobp -= 2; for(i=size;i>0;i--) { if (todo==0) { bp1 = BIG_LONG((list[0])[loffset]); bp2 = BIG_LONG((list[1])[loffset]); loffset++; todo=32; } data = *point; data<<=2; if (bp1<0) data+=1; bp1<<=1; if (bp2<0) data+=2; bp2<<=1; /* data += (bp1 >> 31) + ((bp2 & 0x80000000) >> 30); bp1 <<= 1; bp2 <<= 1; */ *(point++)=data; todo--; } } else{ list -= 4; nobp -= 4; for(i=size;i>0;i--) { if (todo==0) { bp1 = BIG_LONG((list[0])[loffset]); bp2 = BIG_LONG((list[1])[loffset]); bp3 = BIG_LONG((list[2])[loffset]); bp4 = BIG_LONG((list[3])[loffset]); loffset++; todo=32; } data = *point; data<<=4; if (bp1<0) data+=1; bp1<<=1; if (bp2<0) data+=2; bp2<<=1; if (bp3<0) data+=4; bp3<<=1; if (bp4<0) data+=8; bp4<<=1; /* data += (bp1 >> 31) \ + ((bp2 & 0x80000000) >> 30) \ + ((bp3 & 0x80000000) >> 29) \ + ((bp4 & 0x80000000) >> 28); bp1 <<= 1; bp2 <<= 1; bp3 <<= 1; bp4 <<= 1; */ *(point++)=data; todo--; } } } }