line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include "imager.h" |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include "iolayer.h" |
4
|
|
|
|
|
|
|
#ifndef _MSC_VER |
5
|
|
|
|
|
|
|
#include |
6
|
|
|
|
|
|
|
#endif |
7
|
|
|
|
|
|
|
#include |
8
|
|
|
|
|
|
|
#include |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
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 (not implemented) |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
*/ |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
static |
31
|
|
|
|
|
|
|
void |
32
|
1536
|
|
|
|
|
|
interleave(unsigned char *inbuffer,unsigned char *outbuffer,i_img_dim rowsize,int channels) { |
33
|
|
|
|
|
|
|
i_img_dim ind,i; |
34
|
|
|
|
|
|
|
int ch; |
35
|
1536
|
|
|
|
|
|
i=0; |
36
|
1536
|
100
|
|
|
|
|
if (inbuffer == outbuffer) return; /* Check if data is already in interleaved format */ |
37
|
60
|
100
|
|
|
|
|
for (ind=0; ind
|
38
|
192
|
100
|
|
|
|
|
for (ch=0; ch
|
39
|
144
|
|
|
|
|
|
outbuffer[i++] = inbuffer[rowsize*ch+ind]; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
static |
43
|
|
|
|
|
|
|
void |
44
|
1536
|
|
|
|
|
|
expandchannels(unsigned char *inbuffer, unsigned char *outbuffer, |
45
|
|
|
|
|
|
|
i_img_dim xsize, int datachannels, int storechannels) { |
46
|
|
|
|
|
|
|
i_img_dim x; |
47
|
|
|
|
|
|
|
int ch; |
48
|
1536
|
|
|
|
|
|
int copy_chans = storechannels > datachannels ? datachannels : storechannels; |
49
|
1536
|
100
|
|
|
|
|
if (inbuffer == outbuffer) |
50
|
1524
|
|
|
|
|
|
return; /* Check if data is already in expanded format */ |
51
|
60
|
100
|
|
|
|
|
for(x = 0; x < xsize; x++) { |
52
|
192
|
100
|
|
|
|
|
for (ch = 0; ch < copy_chans; ch++) |
53
|
144
|
|
|
|
|
|
outbuffer[x*storechannels+ch] = inbuffer[x*datachannels+ch]; |
54
|
64
|
100
|
|
|
|
|
for (; ch < storechannels; ch++) |
55
|
16
|
|
|
|
|
|
outbuffer[x*storechannels+ch] = 0; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
i_img * |
60
|
23
|
|
|
|
|
|
i_readraw_wiol(io_glue *ig, i_img_dim x, i_img_dim y, int datachannels, int storechannels, int intrl) { |
61
|
|
|
|
|
|
|
i_img* im; |
62
|
|
|
|
|
|
|
ssize_t rc; |
63
|
|
|
|
|
|
|
i_img_dim k; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
unsigned char *inbuffer; |
66
|
|
|
|
|
|
|
unsigned char *ilbuffer; |
67
|
|
|
|
|
|
|
unsigned char *exbuffer; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
size_t inbuflen,ilbuflen,exbuflen; |
70
|
|
|
|
|
|
|
|
71
|
23
|
|
|
|
|
|
i_clear_error(); |
72
|
|
|
|
|
|
|
|
73
|
23
|
|
|
|
|
|
mm_log((1, "i_readraw(ig %p,x %" i_DF ",y %" i_DF ",datachannels %d,storechannels %d,intrl %d)\n", |
74
|
|
|
|
|
|
|
ig, i_DFc(x), i_DFc(y), datachannels, storechannels, intrl)); |
75
|
|
|
|
|
|
|
|
76
|
23
|
100
|
|
|
|
|
if (intrl != 0 && intrl != 1) { |
|
|
100
|
|
|
|
|
|
77
|
1
|
|
|
|
|
|
i_push_error(0, "raw_interleave must be 0 or 1"); |
78
|
1
|
|
|
|
|
|
return NULL; |
79
|
|
|
|
|
|
|
} |
80
|
22
|
50
|
|
|
|
|
if (storechannels < 1 || storechannels > 4) { |
|
|
100
|
|
|
|
|
|
81
|
1
|
|
|
|
|
|
i_push_error(0, "raw_storechannels must be between 1 and 4"); |
82
|
1
|
|
|
|
|
|
return NULL; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
21
|
|
|
|
|
|
im = i_img_empty_ch(NULL,x,y,storechannels); |
86
|
21
|
50
|
|
|
|
|
if (!im) |
87
|
0
|
|
|
|
|
|
return NULL; |
88
|
|
|
|
|
|
|
|
89
|
21
|
|
|
|
|
|
inbuflen = im->xsize*datachannels; |
90
|
21
|
|
|
|
|
|
ilbuflen = inbuflen; |
91
|
21
|
|
|
|
|
|
exbuflen = im->xsize*storechannels; |
92
|
21
|
|
|
|
|
|
inbuffer = (unsigned char*)mymalloc(inbuflen); |
93
|
21
|
|
|
|
|
|
mm_log((1,"inbuflen: %ld, ilbuflen: %ld, exbuflen: %ld.\n", |
94
|
|
|
|
|
|
|
(long)inbuflen, (long)ilbuflen, (long)exbuflen)); |
95
|
|
|
|
|
|
|
|
96
|
21
|
100
|
|
|
|
|
if (intrl==0) ilbuffer = inbuffer; |
97
|
5
|
|
|
|
|
|
else ilbuffer=mymalloc(inbuflen); |
98
|
|
|
|
|
|
|
|
99
|
21
|
100
|
|
|
|
|
if (datachannels==storechannels) exbuffer=ilbuffer; |
100
|
3
|
|
|
|
|
|
else exbuffer= mymalloc(exbuflen); |
101
|
|
|
|
|
|
|
|
102
|
21
|
|
|
|
|
|
k=0; |
103
|
1557
|
100
|
|
|
|
|
while( kysize ) { |
104
|
1538
|
|
|
|
|
|
rc = i_io_read(ig, inbuffer, inbuflen); |
105
|
1538
|
100
|
|
|
|
|
if (rc != inbuflen) { |
106
|
2
|
100
|
|
|
|
|
if (rc < 0) |
107
|
1
|
|
|
|
|
|
i_push_error(0, "error reading file"); |
108
|
|
|
|
|
|
|
else |
109
|
1
|
|
|
|
|
|
i_push_error(0, "premature end of file"); |
110
|
2
|
|
|
|
|
|
i_img_destroy(im); |
111
|
2
|
|
|
|
|
|
myfree(inbuffer); |
112
|
2
|
50
|
|
|
|
|
if (intrl != 0) myfree(ilbuffer); |
113
|
2
|
50
|
|
|
|
|
if (datachannels != storechannels) myfree(exbuffer); |
114
|
2
|
|
|
|
|
|
return NULL; |
115
|
|
|
|
|
|
|
} |
116
|
1536
|
|
|
|
|
|
interleave(inbuffer,ilbuffer,im->xsize,datachannels); |
117
|
1536
|
|
|
|
|
|
expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels); |
118
|
|
|
|
|
|
|
/* FIXME: Do we ever want to save to a virtual image? */ |
119
|
1536
|
|
|
|
|
|
memcpy(&(im->idata[im->xsize*storechannels*k]),exbuffer,exbuflen); |
120
|
1536
|
|
|
|
|
|
k++; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
19
|
|
|
|
|
|
myfree(inbuffer); |
124
|
19
|
100
|
|
|
|
|
if (intrl != 0) myfree(ilbuffer); |
125
|
19
|
100
|
|
|
|
|
if (datachannels != storechannels) myfree(exbuffer); |
126
|
|
|
|
|
|
|
|
127
|
19
|
|
|
|
|
|
i_tags_add(&im->tags, "i_format", 0, "raw", -1, 0); |
128
|
|
|
|
|
|
|
|
129
|
19
|
|
|
|
|
|
return im; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
undef_int |
135
|
14
|
|
|
|
|
|
i_writeraw_wiol(i_img* im, io_glue *ig) { |
136
|
|
|
|
|
|
|
ssize_t rc; |
137
|
|
|
|
|
|
|
|
138
|
14
|
|
|
|
|
|
i_clear_error(); |
139
|
14
|
|
|
|
|
|
mm_log((1,"writeraw(im %p,ig %p)\n", im, ig)); |
140
|
|
|
|
|
|
|
|
141
|
14
|
50
|
|
|
|
|
if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); } |
142
|
14
|
100
|
|
|
|
|
if (!i_img_virtual(im)) { |
143
|
12
|
|
|
|
|
|
rc = i_io_write(ig,im->idata,im->bytes); |
144
|
12
|
100
|
|
|
|
|
if (rc != im->bytes) { |
145
|
1
|
|
|
|
|
|
i_push_error(errno, "Could not write to file"); |
146
|
1
|
|
|
|
|
|
mm_log((1,"i_writeraw: Couldn't write to file\n")); |
147
|
1
|
|
|
|
|
|
return(0); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} else { |
150
|
2
|
50
|
|
|
|
|
if (im->type == i_direct_type) { |
151
|
|
|
|
|
|
|
/* just save it as 8-bits, maybe support saving higher bit count |
152
|
|
|
|
|
|
|
raw images later */ |
153
|
2
|
|
|
|
|
|
size_t line_size = im->xsize * im->channels; |
154
|
2
|
|
|
|
|
|
unsigned char *data = mymalloc(line_size); |
155
|
|
|
|
|
|
|
|
156
|
2
|
|
|
|
|
|
i_img_dim y = 0; |
157
|
2
|
|
|
|
|
|
rc = line_size; |
158
|
302
|
50
|
|
|
|
|
while (rc == line_size && y < im->ysize) { |
|
|
100
|
|
|
|
|
|
159
|
300
|
|
|
|
|
|
i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels); |
160
|
300
|
|
|
|
|
|
rc = i_io_write(ig, data, line_size); |
161
|
300
|
|
|
|
|
|
++y; |
162
|
|
|
|
|
|
|
} |
163
|
2
|
50
|
|
|
|
|
if (rc != line_size) { |
164
|
0
|
|
|
|
|
|
i_push_error(errno, "write error"); |
165
|
0
|
|
|
|
|
|
return 0; |
166
|
|
|
|
|
|
|
} |
167
|
2
|
|
|
|
|
|
myfree(data); |
168
|
|
|
|
|
|
|
} else { |
169
|
|
|
|
|
|
|
/* paletted image - assumes the caller puts the palette somewhere |
170
|
|
|
|
|
|
|
else |
171
|
|
|
|
|
|
|
*/ |
172
|
0
|
|
|
|
|
|
size_t line_size = sizeof(i_palidx) * im->xsize; |
173
|
0
|
|
|
|
|
|
i_palidx *data = mymalloc(sizeof(i_palidx) * im->xsize); |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
i_img_dim y = 0; |
176
|
0
|
|
|
|
|
|
rc = line_size; |
177
|
0
|
0
|
|
|
|
|
while (rc == line_size && y < im->ysize) { |
|
|
0
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
i_gpal(im, 0, im->xsize, y, data); |
179
|
0
|
|
|
|
|
|
rc = i_io_write(ig, data, line_size); |
180
|
0
|
|
|
|
|
|
++y; |
181
|
|
|
|
|
|
|
} |
182
|
0
|
|
|
|
|
|
myfree(data); |
183
|
0
|
0
|
|
|
|
|
if (rc != line_size) { |
184
|
0
|
|
|
|
|
|
i_push_error(errno, "write error"); |
185
|
0
|
|
|
|
|
|
return 0; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
13
|
100
|
|
|
|
|
if (i_io_close(ig)) |
191
|
3
|
|
|
|
|
|
return 0; |
192
|
|
|
|
|
|
|
|
193
|
10
|
|
|
|
|
|
return(1); |
194
|
|
|
|
|
|
|
} |