Skip to content
Newer
Older
100644 168 lines (136 sloc) 4.32 KB
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
1 #include "image.h"
2 #include <stdio.h>
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
3 #include "iolayer.h"
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7 #include <string.h>
faa9b3e Egads
Tony Cook authored Aug 13, 2001
8 #include <errno.h>
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
9
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
10
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
11
12 /*
13
14 Image loader for raw files.
15
16 This is a barebones raw loader...
17
18 fd: filedescriptor
19 x: xsize
20 y: ysize
21 datachannels: the number of channels the file contains
22 storechannels: the bitmap of channels we will read
23 intrl: interlace flag,
24 0 = sample interleaving
25 1 = line interleaving
26 2 = image interleaving
27
28 */
29
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
30 static
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
31 void
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
32 interleave(unsigned char *inbuffer,unsigned char *outbuffer,int rowsize,int channels) {
33 int ch,ind,i;
34 i=0;
35 if (inbuffer == outbuffer) return; /* Check if data is already in interleaved format */
36 for (ind=0; ind<rowsize; ind++)
37 for (ch=0; ch<channels; ch++)
38 outbuffer[i++] = inbuffer[rowsize*ch+ind];
39 }
40
41 static
42 void
43 expandchannels(unsigned char *inbuffer, unsigned char *outbuffer,
44 int chunks, int datachannels, int storechannels) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
45 int ch,i;
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
46 if (inbuffer == outbuffer) return; /* Check if data is already in expanded format */
47 for(ch=0; ch<chunks; ch++)
48 for (i=0; i<storechannels; i++)
49 outbuffer[ch*storechannels+i] = inbuffer[ch*datachannels+i];
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
50 }
51
52 i_img *
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
53 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
54 i_img* im;
55 int rc,k;
56
57 unsigned char *inbuffer;
58 unsigned char *ilbuffer;
59 unsigned char *exbuffer;
60
61 int inbuflen,ilbuflen,exbuflen;
62
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
63 io_glue_commit_types(ig);
64 mm_log((1, "i_readraw(ig %p,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",
65 ig, x, y, datachannels, storechannels, intrl));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
66
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
67 im = i_img_empty_ch(NULL,x,y,storechannels);
50dc291 - i_readraw_wiol() now checks for image creation failure
Tony Cook authored Dec 2, 2004
68 if (!im)
69 return NULL;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
70
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
71 inbuflen = im->xsize*datachannels;
72 ilbuflen = inbuflen;
73 exbuflen = im->xsize*storechannels;
74 inbuffer = (unsigned char*)mymalloc(inbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
75 mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
76
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
77 if (intrl==0) ilbuffer = inbuffer;
78 else ilbuffer=mymalloc(inbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
79
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
80 if (datachannels==storechannels) exbuffer=ilbuffer;
81 else exbuffer= mymalloc(exbuflen);
82
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
83 k=0;
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
84 while( k<im->ysize ) {
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
85 rc = ig->readcb(ig, inbuffer, inbuflen);
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
86 if (rc != inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
87 interleave(inbuffer,ilbuffer,im->xsize,datachannels);
88 expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
89 /* FIXME: Do we ever want to save to a virtual image? */
faa9b3e Egads
Tony Cook authored Aug 13, 2001
90 memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
91 k++;
92 }
93
94 myfree(inbuffer);
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
95 if (intrl != 0) myfree(ilbuffer);
96 if (datachannels != storechannels) myfree(exbuffer);
50dc291 - i_readraw_wiol() now checks for image creation failure
Tony Cook authored Dec 2, 2004
97
98 i_tags_add(&im->tags, "i_format", 0, "raw", -1, 0);
99
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
100 return im;
101 }
102
103
104
105 undef_int
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
106 i_writeraw_wiol(i_img* im, io_glue *ig) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
107 int rc;
faa9b3e Egads
Tony Cook authored Aug 13, 2001
108
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
109 io_glue_commit_types(ig);
faa9b3e Egads
Tony Cook authored Aug 13, 2001
110 i_clear_error();
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
111 mm_log((1,"writeraw(im %p,ig %p)\n", im, ig));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
112
113 if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
faa9b3e Egads
Tony Cook authored Aug 13, 2001
114 if (!im->virtual) {
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
115 rc = ig->writecb(ig,im->idata,im->bytes);
116 if (rc != im->bytes) {
faa9b3e Egads
Tony Cook authored Aug 13, 2001
117 i_push_error(errno, "Could not write to file");
118 mm_log((1,"i_writeraw: Couldn't write to file\n"));
119 return(0);
120 }
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
121 } else {
faa9b3e Egads
Tony Cook authored Aug 13, 2001
122 int y;
123
124 if (im->type == i_direct_type) {
125 /* just save it as 8-bits, maybe support saving higher bit count
126 raw images later */
127 int line_size = im->xsize * im->channels;
128 unsigned char *data = mymalloc(line_size);
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored Nov 1, 2001
129
130 int y = 0;
131 rc = line_size;
132 while (rc == line_size && y < im->ysize) {
133 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
134 rc = ig->writecb(ig, data, line_size);
135 ++y;
faa9b3e Egads
Tony Cook authored Aug 13, 2001
136 }
137 if (rc != line_size) {
138 i_push_error(errno, "write error");
139 return 0;
140 }
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored Nov 1, 2001
141 myfree(data);
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
142 } else {
faa9b3e Egads
Tony Cook authored Aug 13, 2001
143 /* paletted image - assumes the caller puts the palette somewhere
144 else
145 */
146 int line_size = sizeof(i_palidx) * im->xsize;
147 i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize);
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored Nov 1, 2001
148
149 int y = 0;
150 rc = line_size;
151 while (rc == line_size && y < im->ysize) {
152 i_gpal(im, 0, im->xsize, y, data);
153 rc = ig->writecb(ig, data, line_size);
154 ++y;
faa9b3e Egads
Tony Cook authored Aug 13, 2001
155 }
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored Nov 1, 2001
156 myfree(data);
faa9b3e Egads
Tony Cook authored Aug 13, 2001
157 if (rc != line_size) {
158 i_push_error(errno, "write error");
159 return 0;
160 }
161 }
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored Jun 6, 2001
162 }
10461f9 enhanced iolayer
Tony Cook authored Jan 4, 2002
163
164 ig->closecb(ig);
165
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
166 return(1);
167 }
Something went wrong with that request. Please try again.