Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 158 lines (132 sloc) 4.267 kB
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
1 #include "image.h"
2 #include <stdio.h>
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
3 #include "iolayer.h"
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7 #include <string.h>
faa9b3e Egads
Tony Cook authored
8 #include <errno.h>
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
9
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
10
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
11 #define TRUE 1
12 #define FALSE 0
13
14 #define BSIZ 100*BUFSIZ
15
16 /*
17
18 Image loader for raw files.
19
20 This is a barebones raw loader...
21
22 fd: filedescriptor
23 x: xsize
24 y: ysize
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
29 1 = line interleaving
30 2 = image interleaving
31
32 */
33
34 void
35 expandchannels(unsigned char *inbuffer,unsigned char *outbuffer,int chunks,int datachannels,int storechannels) {
36 int ch,i;
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];
39 }
40
41 i_img *
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
42 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
43 i_img* im;
44 int rc,k;
45
46 unsigned char *inbuffer;
47 unsigned char *ilbuffer;
48 unsigned char *exbuffer;
49
50 int inbuflen,ilbuflen,exbuflen;
51
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
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));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
55
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
56 im = i_img_empty_ch(NULL,x,y,storechannels);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
57
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
58 inbuflen = im->xsize*datachannels;
59 ilbuflen = inbuflen;
60 exbuflen = im->xsize*storechannels;
61 inbuffer = (unsigned char*)mymalloc(inbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
62 mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen));
63
64 if (intrl==0) ilbuffer=inbuffer; else ilbuffer=(unsigned char*)mymalloc(inbuflen);
65 if (datachannels==storechannels) exbuffer=ilbuffer; else exbuffer=(unsigned char*)mymalloc(exbuflen);
66
67 k=0;
68 while(k<im->ysize) {
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
69 rc = ig->readcb(ig, inbuffer, inbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
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);
faa9b3e Egads
Tony Cook authored
73 /* FIXME? Do we ever want to save to a virtual image? */
74 memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
75 k++;
76 }
77
78 myfree(inbuffer);
79 if (intrl!=0) myfree(ilbuffer);
80 if (datachannels!=storechannels) myfree(exbuffer);
81 return im;
82 }
83
84
85
86 undef_int
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
87 i_writeraw_wiol(i_img* im, io_glue *ig) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
88 int rc;
faa9b3e Egads
Tony Cook authored
89
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
90 io_glue_commit_types(ig);
faa9b3e Egads
Tony Cook authored
91 i_clear_error();
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
92 mm_log((1,"writeraw(im %p,ig %p)\n", im, ig));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
93
94 if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); }
faa9b3e Egads
Tony Cook authored
95 if (!im->virtual) {
96 rc=ig->writecb(ig,im->idata,im->bytes);
97 if (rc!=im->bytes) {
98 i_push_error(errno, "Could not write to file");
99 mm_log((1,"i_writeraw: Couldn't write to file\n"));
100 return(0);
101 }
102 }
103 else {
104 int y;
105
106 if (im->type == i_direct_type) {
107 /* just save it as 8-bits, maybe support saving higher bit count
108 raw images later */
109 int line_size = im->xsize * im->channels;
110 unsigned char *data = mymalloc(line_size);
111 if (data) {
112 int y = 0;
113 rc = 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);
117 ++y;
118 }
119 }
120 else {
121 i_push_error(0, "Out of memory");
122 return 0;
123 }
124 if (rc != line_size) {
125 i_push_error(errno, "write error");
126 return 0;
127 }
128 }
129 else {
130 /* paletted image - assumes the caller puts the palette somewhere
131 else
132 */
133 int line_size = sizeof(i_palidx) * im->xsize;
134 i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize);
135 if (data) {
136 int y = 0;
137 rc = line_size;
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);
141 ++y;
142 }
143 myfree(data);
144 }
145 else {
146 i_push_error(0, "Out of memory");
147 return 0;
148 }
149 if (rc != line_size) {
150 i_push_error(errno, "write error");
151 return 0;
152 }
153 }
895dbd3 Replaced i_readraw() and i_writeraw() with the equivalent _wiol funct…
Arnar Mar Hrafnkelsson authored
154 }
faa9b3e Egads
Tony Cook authored
155
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
156 return(1);
157 }
Something went wrong with that request. Please try again.