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