4 convert.im - image conversions
8 out = i_convert(srcimage, coeff, outchans, inchans)
12 Converts images from one format to another, typically in this case for
13 converting from RGBA to greyscale and back.
20 #define IMAGER_NO_CONTEXT
24 /* channels to copy */
26 int from[MAXCHANNELS];
29 /* channels to zero */
31 int zero[MAXCHANNELS];
33 /* channels to set to maxsample */
39 is_channel_copy(i_img *im, const double *coeff,
40 int outchan, int inchan,
41 struct chan_copy *info);
44 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info);
47 =item i_convert(src, coeff, outchan, inchan)
49 Converts the image src into another image.
51 coeff contains the co-efficients of an outchan x inchan matrix, for
54 coeff[0], coeff[1] ...
55 im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1]
56 ... coeff[inchan*outchan-1]
58 If im has the wrong number of channels or is the wrong size then
59 i_convert() will re-create it.
61 Now handles images with more than 8-bits/sample.
67 i_convert(i_img *src, const double *coeff, int outchan, int inchan) {
68 double work[MAXCHANNELS];
75 im_log((aIMCTX,1,"i_convert(im %p, src %p, coeff %p,outchan %d, inchan %d)\n",
76 im, src, coeff, outchan, inchan));
78 im_clear_error(aIMCTX);
81 if (ilimit > src->channels)
82 ilimit = src->channels;
83 if (outchan > MAXCHANNELS) {
84 im_push_error(aIMCTX, 0, "cannot have outchan > MAXCHANNELS");
88 if (src->type == i_direct_type) {
89 struct chan_copy info;
90 im = i_sametype_chans(src, src->xsize, src->ysize, outchan);
92 if (is_channel_copy(src, coeff, outchan, inchan, &info)) {
93 return convert_via_copy(im, src, &info);
96 #code src->bits <= i_8_bits
99 /* we can always allocate a single scanline of i_color */
100 vals = mymalloc(sizeof(IM_COLOR) * src->xsize); /* checked 04Jul05 tonyc */
101 for (y = 0; y < src->ysize; ++y) {
102 IM_GLIN(src, 0, src->xsize, y, vals);
103 for (x = 0; x < src->xsize; ++x) {
104 for (j = 0; j < outchan; ++j) {
106 for (i = 0; i < ilimit; ++i) {
107 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
110 work[j] += coeff[i+inchan*j] * IM_SAMPLE_MAX;
113 for (j = 0; j < outchan; ++j) {
115 vals[x].channel[j] = 0;
116 else if (work[j] >= IM_SAMPLE_MAX)
117 vals[x].channel[j] = IM_SAMPLE_MAX;
119 vals[x].channel[j] = work[j];
122 IM_PLIN(im, 0, src->xsize, y, vals);
135 im = im_img_pal_new(aIMCTX, src->xsize, src->ysize, outchan,
138 /* just translate the color table */
139 count = i_colorcount(src);
140 outcount = i_colorcount(im);
141 /* color table allocated for image, so it must fit */
142 colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */
143 i_getcolors(src, 0, colors, count);
144 for (index = 0; index < count; ++index) {
145 for (j = 0; j < outchan; ++j) {
147 for (i = 0; i < ilimit; ++i) {
148 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
151 work[j] += coeff[i+inchan*j] * 255.9;
154 for (j = 0; j < outchan; ++j) {
156 colors[index].channel[j] = 0;
157 else if (work[j] >= 255)
158 colors[index].channel[j] = 255;
160 colors[index].channel[j] = work[j];
163 if (count < outcount) {
164 i_setcolors(im, 0, colors, count);
167 i_setcolors(im, 0, colors, outcount);
168 i_addcolors(im, colors, count-outcount);
170 /* and copy the indicies */
171 /* i_palidx is always unsigned char and will never be bigger than short
172 and since a line of 4-byte i_colors can fit then a line of i_palidx
174 vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */
175 for (y = 0; y < im->ysize; ++y) {
176 i_gpal(src, 0, im->xsize, y, vals);
177 i_ppal(im, 0, im->xsize, y, vals);
187 =item is_channel_copy(coeff, outchan, inchan, chan_copy_info)
189 Test if the coefficients represent just copying channels around, and
190 initialize lists of the channels to copy, zero or set to max.
196 int is_channel_copy(i_img *im, const double *coeff, int outchan, int inchan,
197 struct chan_copy *info) {
198 int srcchan[MAXCHANNELS];
199 int onechan[MAXCHANNELS];
201 int ilimit = im->channels > inchan ? inchan : im->channels;
203 for (j = 0; j < outchan; ++j) {
208 for (j = 0; j < outchan; ++j) {
209 for (i = 0; i < ilimit; ++i) {
210 if (coeff[i+inchan*j] == 1.0) {
211 if (srcchan[j] != -1) {
212 /* from two or more channels, not a copy */
217 else if (coeff[i+inchan*j]) {
218 /* some other non-zero value, not a copy */
223 if (coeff[i+inchan*j] == 1.0) {
224 if (srcchan[j] != -1) {
230 else if (coeff[i+inchan*j]) {
231 /* some other non-zero value, not a copy */
237 /* build our working data structures */
238 info->copy_count = info->zero_count = info->one_count = 0;
239 for (j = 0; j < outchan; ++j) {
240 if (srcchan[j] != -1) {
241 info->from[info->copy_count] = srcchan[j];
242 info->to[info->copy_count] = j;
245 else if (onechan[j]) {
246 info->one[info->one_count] = j;
250 info->zero[info->zero_count] = j;
257 for (i = 0; i < info->copy_count; ++i) {
258 printf("From %d to %d\n", info->from[i], info->to[i]);
260 for (i = 0; i < info->one_count; ++i) {
261 printf("One %d\n", info->one[i]);
263 for (i = 0; i < info->zero_count; ++i) {
264 printf("Zero %d\n", info->zero[i]);
274 =item convert_via_copy(im, src, chan_copy_info)
276 Perform a convert that only requires channel copies.
282 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info) {
283 #code src->bits <= i_8_bits
284 IM_COLOR *in_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
285 IM_COLOR *out_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
288 IM_COLOR *inp, *outp;
290 for (y = 0; y < src->ysize; ++y) {
291 IM_GLIN(src, 0, src->xsize, y, in_line);
295 for (x = 0; x < src->xsize; ++x) {
296 for (i = 0; i < info->copy_count; ++i) {
297 outp->channel[info->to[i]] = inp->channel[info->from[i]];
299 for (i = 0; i < info->one_count; ++i) {
300 outp->channel[info->one[i]] = IM_SAMPLE_MAX;
302 for (i = 0; i < info->zero_count; ++i) {
303 outp->channel[info->zero[i]] = 0;
309 IM_PLIN(im, 0, src->xsize, y, out_line);
328 Tony Cook <tony@develop-help.com>