Skip to content
Newer
Older
100644 167 lines (135 sloc) 3.89 KB
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
1 /*
2 =head1 NAME
3
6286932 reorganize convert.c to convert.im
Tony Cook authored
4 convert.im - image conversions
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
5
6 =head1 SYNOPSIS
7
8 i_convert(outimage, srcimage, coeff, outchans, inchans)
9
10 =head1 DESCRIPTION
11
12 Converts images from one format to another, typically in this case for
13 converting from RGBA to greyscale and back.
14
15 =over
16
17 =cut
18 */
19
92bda63 - start of external Imager API access:
Tony Cook authored
20 #include "imager.h"
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
21
22
23 /*
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
24 =item i_convert(src, coeff, outchan, inchan)
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
25
26 Converts the image src into another image.
27
28 coeff contains the co-efficients of an outchan x inchan matrix, for
29 each output pixel:
30
31 coeff[0], coeff[1] ...
32 im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1]
33 ... coeff[inchan*outchan-1]
34
35 If im has the wrong number of channels or is the wrong size then
36 i_convert() will re-create it.
37
faa9b3e Egads
Tony Cook authored
38 Now handles images with more than 8-bits/sample.
39
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
40 =cut
41 */
42
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
43 i_img *
6286932 reorganize convert.c to convert.im
Tony Cook authored
44 i_convert(i_img *src, const double *coeff, int outchan, int inchan) {
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
45 int x, y;
46 int i, j;
a743c0a Removed a bunch of unused variables and fixed an attempt to print out a
Arnar Mar Hrafnkelsson authored
47 int ilimit;
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
48 double work[MAXCHANNELS];
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
49 i_img *im = NULL;
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
50
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
51 mm_log((1,"i_convert(src %p, coeff %p,outchan %d, inchan %d)\n",im,src, coeff,outchan, inchan));
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
52
53 i_clear_error();
54
55 ilimit = inchan;
56 if (ilimit > src->channels)
57 ilimit = src->channels;
58 if (outchan > MAXCHANNELS) {
59 i_push_error(0, "cannot have outchan > MAXCHANNELS");
60 return 0;
61 }
62
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
63 if (src->type == i_direct_type) {
64 im = i_sametype_chans(src, src->xsize, src->ysize, outchan);
6286932 reorganize convert.c to convert.im
Tony Cook authored
65 #code src->bits <= i_8_bits
66 IM_COLOR *vals;
67
68 /* we can always allocate a single scanline of i_color */
69 vals = mymalloc(sizeof(IM_COLOR) * src->xsize); /* checked 04Jul05 tonyc */
70 for (y = 0; y < src->ysize; ++y) {
71 IM_GLIN(src, 0, src->xsize, y, vals);
72 for (x = 0; x < src->xsize; ++x) {
73 for (j = 0; j < outchan; ++j) {
74 work[j] = 0;
75 for (i = 0; i < ilimit; ++i) {
76 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
77 }
78 if (i < inchan) {
79 work[j] += coeff[i+inchan*j] * IM_SAMPLE_MAX;
80 }
81 }
82 for (j = 0; j < outchan; ++j) {
83 if (work[j] < 0)
84 vals[x].channel[j] = 0;
85 else if (work[j] >= IM_SAMPLE_MAX)
86 vals[x].channel[j] = IM_SAMPLE_MAX;
87 else
88 vals[x].channel[j] = work[j];
89 }
faa9b3e Egads
Tony Cook authored
90 }
6286932 reorganize convert.c to convert.im
Tony Cook authored
91 IM_PLIN(im, 0, src->xsize, y, vals);
faa9b3e Egads
Tony Cook authored
92 }
6286932 reorganize convert.c to convert.im
Tony Cook authored
93 myfree(vals);
94 #/code
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
95 }
faa9b3e Egads
Tony Cook authored
96 else {
97 int count;
98 int outcount;
99 int index;
100 i_color *colors;
101 i_palidx *vals;
102
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
103 im = i_img_pal_new(src->xsize, src->ysize, outchan,
104 i_maxcolors(src));
105
faa9b3e Egads
Tony Cook authored
106 /* just translate the color table */
107 count = i_colorcount(src);
108 outcount = i_colorcount(im);
f0960b1 - added integer overflow checks to many memory allocation calls
Tony Cook authored
109 /* color table allocated for image, so it must fit */
110 colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */
faa9b3e Egads
Tony Cook authored
111 i_getcolors(src, 0, colors, count);
112 for (index = 0; index < count; ++index) {
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
113 for (j = 0; j < outchan; ++j) {
faa9b3e Egads
Tony Cook authored
114 work[j] = 0;
115 for (i = 0; i < ilimit; ++i) {
116 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
117 }
118 if (i < inchan) {
119 work[j] += coeff[i+inchan*j] * 255.9;
120 }
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
121 }
122 for (j = 0; j < outchan; ++j) {
faa9b3e Egads
Tony Cook authored
123 if (work[j] < 0)
124 colors[index].channel[j] = 0;
125 else if (work[j] >= 255)
126 colors[index].channel[j] = 255;
127 else
128 colors[index].channel[j] = work[j];
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
129 }
130 }
faa9b3e Egads
Tony Cook authored
131 if (count < outcount) {
132 i_setcolors(im, 0, colors, count);
133 }
134 else {
135 i_setcolors(im, 0, colors, outcount);
136 i_addcolors(im, colors, count-outcount);
137 }
138 /* and copy the indicies */
f0960b1 - added integer overflow checks to many memory allocation calls
Tony Cook authored
139 /* i_palidx is always unsigned char and will never be bigger than short
140 and since a line of 4-byte i_colors can fit then a line of i_palidx
141 will fit */
142 vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */
faa9b3e Egads
Tony Cook authored
143 for (y = 0; y < im->ysize; ++y) {
144 i_gpal(src, 0, im->xsize, y, vals);
145 i_ppal(im, 0, im->xsize, y, vals);
146 }
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored
147 myfree(vals);
148 myfree(colors);
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
149 }
faa9b3e Egads
Tony Cook authored
150
d5477d3 - Finished/rewrote Arnar's old SGI RGB file format support, so Imager
Tony Cook authored
151 return im;
f5991c0 implement the convert() method for converting between numbers of chan…
Tony Cook authored
152 }
153
154 /*
155 =back
156
157 =head1 SEE ALSO
158
159 Imager(3)
160
161 =head1 AUTHOR
162
163 Tony Cook <tony@develop-help.com>
164
165 =cut
166 */
Something went wrong with that request. Please try again.