14 #define BSIZ 100*BUFSIZ
18 Image loader for raw files.
20 This is a barebones raw loader...
25 datachannels: the number of channels the file contains
26 storechannels: the bitmap of channels we will read
27 intrl: interlace flag,
28 0 = sample interleaving
30 2 = image interleaving
35 expandchannels(unsigned char *inbuffer,unsigned char *outbuffer,int chunks,int datachannels,int storechannels) {
37 if (inbuffer==outbuffer) return; /* Check if data is already in expanded format */
38 for(ch=0;ch<chunks;ch++) for (i=0;i<storechannels;i++) outbuffer[ch*storechannels+i]=inbuffer[ch*datachannels+i];
42 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
46 unsigned char *inbuffer;
47 unsigned char *ilbuffer;
48 unsigned char *exbuffer;
50 int inbuflen,ilbuflen,exbuflen;
52 io_glue_commit_types(ig);
53 mm_log((1, "i_readraw(ig %p,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",
54 ig, x, y, datachannels, storechannels, intrl));
56 im = i_img_empty_ch(NULL,x,y,storechannels);
58 inbuflen = im->xsize*datachannels;
60 exbuflen = im->xsize*storechannels;
61 inbuffer = (unsigned char*)mymalloc(inbuflen);
62 mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
64 if (intrl==0) ilbuffer=inbuffer; else ilbuffer=(unsigned char*)mymalloc(inbuflen);
65 if (datachannels==storechannels) exbuffer=ilbuffer; else exbuffer=(unsigned char*)mymalloc(exbuflen);
69 rc = ig->readcb(ig, inbuffer, inbuflen);
70 if (rc!=inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
71 interleave(inbuffer,ilbuffer,im->xsize,datachannels);
72 expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
73 /* FIXME? Do we ever want to save to a virtual image? */
74 memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen);
79 if (intrl!=0) myfree(ilbuffer);
80 if (datachannels!=storechannels) myfree(exbuffer);
87 i_writeraw_wiol(i_img* im, io_glue *ig) {
90 io_glue_commit_types(ig);
92 mm_log((1,"writeraw(im %p,ig %p)\n", im, ig));
94 if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
96 rc=ig->writecb(ig,im->idata,im->bytes);
98 i_push_error(errno, "Could not write to file");
99 mm_log((1,"i_writeraw: Couldn't write to file\n"));
106 if (im->type == i_direct_type) {
107 /* just save it as 8-bits, maybe support saving higher bit count
109 int line_size = im->xsize * im->channels;
110 unsigned char *data = mymalloc(line_size);
114 while (rc == line_size && y < im->ysize) {
115 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
116 rc = ig->writecb(ig, data, line_size);
121 i_push_error(0, "Out of memory");
124 if (rc != line_size) {
125 i_push_error(errno, "write error");
130 /* paletted image - assumes the caller puts the palette somewhere
133 int line_size = sizeof(i_palidx) * im->xsize;
134 i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize);
138 while (rc == line_size && y < im->ysize) {
139 i_gpal(im, 0, im->xsize, y, data);
140 rc = ig->writecb(ig, data, line_size);
146 i_push_error(0, "Out of memory");
149 if (rc != line_size) {
150 i_push_error(errno, "write error");