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.
23 /* channels to copy */
25 int from[MAXCHANNELS];
28 /* channels to zero */
30 int zero[MAXCHANNELS];
32 /* channels to set to maxsample */
38 is_channel_copy(i_img *im, const double *coeff,
39 int outchan, int inchan,
40 struct chan_copy *info);
43 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info);
46 =item i_convert(src, coeff, outchan, inchan)
48 Converts the image src into another image.
50 coeff contains the co-efficients of an outchan x inchan matrix, for
53 coeff[0], coeff[1] ...
54 im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1]
55 ... coeff[inchan*outchan-1]
57 If im has the wrong number of channels or is the wrong size then
58 i_convert() will re-create it.
60 Now handles images with more than 8-bits/sample.
66 i_convert(i_img *src, const double *coeff, int outchan, int inchan) {
67 double work[MAXCHANNELS];
73 mm_log((1,"i_convert(im %p, src %p, coeff %p,outchan %d, inchan %d)\n",
74 im, src, coeff, outchan, inchan));
79 if (ilimit > src->channels)
80 ilimit = src->channels;
81 if (outchan > MAXCHANNELS) {
82 i_push_error(0, "cannot have outchan > MAXCHANNELS");
86 if (src->type == i_direct_type) {
87 struct chan_copy info;
88 im = i_sametype_chans(src, src->xsize, src->ysize, outchan);
90 if (is_channel_copy(src, coeff, outchan, inchan, &info)) {
91 return convert_via_copy(im, src, &info);
94 #code src->bits <= i_8_bits
97 /* we can always allocate a single scanline of i_color */
98 vals = mymalloc(sizeof(IM_COLOR) * src->xsize); /* checked 04Jul05 tonyc */
99 for (y = 0; y < src->ysize; ++y) {
100 IM_GLIN(src, 0, src->xsize, y, vals);
101 for (x = 0; x < src->xsize; ++x) {
102 for (j = 0; j < outchan; ++j) {
104 for (i = 0; i < ilimit; ++i) {
105 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
108 work[j] += coeff[i+inchan*j] * IM_SAMPLE_MAX;
111 for (j = 0; j < outchan; ++j) {
113 vals[x].channel[j] = 0;
114 else if (work[j] >= IM_SAMPLE_MAX)
115 vals[x].channel[j] = IM_SAMPLE_MAX;
117 vals[x].channel[j] = work[j];
120 IM_PLIN(im, 0, src->xsize, y, vals);
133 im = i_img_pal_new(src->xsize, src->ysize, outchan,
136 /* just translate the color table */
137 count = i_colorcount(src);
138 outcount = i_colorcount(im);
139 /* color table allocated for image, so it must fit */
140 colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */
141 i_getcolors(src, 0, colors, count);
142 for (index = 0; index < count; ++index) {
143 for (j = 0; j < outchan; ++j) {
145 for (i = 0; i < ilimit; ++i) {
146 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
149 work[j] += coeff[i+inchan*j] * 255.9;
152 for (j = 0; j < outchan; ++j) {
154 colors[index].channel[j] = 0;
155 else if (work[j] >= 255)
156 colors[index].channel[j] = 255;
158 colors[index].channel[j] = work[j];
161 if (count < outcount) {
162 i_setcolors(im, 0, colors, count);
165 i_setcolors(im, 0, colors, outcount);
166 i_addcolors(im, colors, count-outcount);
168 /* and copy the indicies */
169 /* i_palidx is always unsigned char and will never be bigger than short
170 and since a line of 4-byte i_colors can fit then a line of i_palidx
172 vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */
173 for (y = 0; y < im->ysize; ++y) {
174 i_gpal(src, 0, im->xsize, y, vals);
175 i_ppal(im, 0, im->xsize, y, vals);
185 =item is_channel_copy(coeff, outchan, inchan, chan_copy_info)
187 Test if the coefficients represent just copying channels around, and
188 initialize lists of the channels to copy, zero or set to max.
194 int is_channel_copy(i_img *im, const double *coeff, int outchan, int inchan,
195 struct chan_copy *info) {
196 int srcchan[MAXCHANNELS];
197 int onechan[MAXCHANNELS];
199 int ilimit = im->channels > inchan ? inchan : im->channels;
201 for (j = 0; j < outchan; ++j) {
206 for (j = 0; j < outchan; ++j) {
207 for (i = 0; i < ilimit; ++i) {
208 if (coeff[i+inchan*j] == 1.0) {
209 if (srcchan[j] != -1) {
210 /* from two or more channels, not a copy */
215 else if (coeff[i+inchan*j]) {
216 /* some other non-zero value, not a copy */
221 if (coeff[i+inchan*j] == 1.0) {
222 if (srcchan[j] != -1) {
228 else if (coeff[i+inchan*j]) {
229 /* some other non-zero value, not a copy */
235 /* build our working data structures */
236 info->copy_count = info->zero_count = info->one_count = 0;
237 for (j = 0; j < outchan; ++j) {
238 if (srcchan[j] != -1) {
239 info->from[info->copy_count] = srcchan[j];
240 info->to[info->copy_count] = j;
243 else if (onechan[j]) {
244 info->one[info->one_count] = j;
248 info->zero[info->zero_count] = j;
255 for (i = 0; i < info->copy_count; ++i) {
256 printf("From %d to %d\n", info->from[i], info->to[i]);
258 for (i = 0; i < info->one_count; ++i) {
259 printf("One %d\n", info->one[i]);
261 for (i = 0; i < info->zero_count; ++i) {
262 printf("Zero %d\n", info->zero[i]);
272 =item convert_via_copy(im, src, chan_copy_info)
274 Perform a convert that only requires channel copies.
280 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info) {
281 #code src->bits <= i_8_bits
282 IM_COLOR *in_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
283 IM_COLOR *out_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
286 IM_COLOR *inp, *outp;
288 for (y = 0; y < src->ysize; ++y) {
289 IM_GLIN(src, 0, src->xsize, y, in_line);
293 for (x = 0; x < src->xsize; ++x) {
294 for (i = 0; i < info->copy_count; ++i) {
295 outp->channel[info->to[i]] = inp->channel[info->from[i]];
297 for (i = 0; i < info->one_count; ++i) {
298 outp->channel[info->one[i]] = IM_SAMPLE_MAX;
300 for (i = 0; i < info->zero_count; ++i) {
301 outp->channel[info->zero[i]] = 0;
307 IM_PLIN(im, 0, src->xsize, y, out_line);
326 Tony Cook <tony@develop-help.com>