4 convert.c - image conversions
8 i_convert(outimage, 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.
24 =item i_convert(im, src, coeff, outchan, inchan)
26 Converts the image src into another image.
28 coeff contains the co-efficients of an outchan x inchan matrix, for
31 coeff[0], coeff[1] ...
32 im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1]
33 ... coeff[inchan*outchan-1]
35 If im has the wrong number of channels or is the wrong size then
36 i_convert() will re-create it.
38 Now handles images with more than 8-bits/sample.
44 i_convert(i_img *im, i_img *src, float *coeff, int outchan, int inchan)
49 double work[MAXCHANNELS];
51 mm_log((1,"i_convert(im %p, src, %p, coeff %p,outchan %d, inchan %d)\n",im,src, coeff,outchan, inchan));
56 if (ilimit > src->channels)
57 ilimit = src->channels;
58 if (outchan > MAXCHANNELS) {
59 i_push_error(0, "cannot have outchan > MAXCHANNELS");
63 if (im->type == i_direct_type || src->type == i_direct_type) {
64 /* first check the output image */
65 if (im->channels != outchan || im->xsize != src->xsize
66 || im->ysize != src->ysize) {
68 i_img_empty_ch(im, src->xsize, src->ysize, outchan);
70 if (im->bits == i_8_bits && src->bits == i_8_bits) {
73 vals = mymalloc(sizeof(i_color) * src->xsize);
74 for (y = 0; y < src->ysize; ++y) {
75 i_glin(src, 0, src->xsize, y, vals);
76 for (x = 0; x < src->xsize; ++x) {
77 for (j = 0; j < outchan; ++j) {
79 for (i = 0; i < ilimit; ++i) {
80 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
83 work[j] += coeff[i+inchan*j] * 255.9;
86 for (j = 0; j < outchan; ++j) {
88 vals[x].channel[j] = 0;
89 else if (work[j] >= 256)
90 vals[x].channel[j] = 255;
92 vals[x].channel[j] = work[j];
95 i_plin(im, 0, src->xsize, y, vals);
102 vals = mymalloc(sizeof(i_fcolor) * src->xsize);
103 for (y = 0; y < src->ysize; ++y) {
104 i_glinf(src, 0, src->xsize, y, vals);
105 for (x = 0; x < src->xsize; ++x) {
106 for (j = 0; j < outchan; ++j) {
108 for (i = 0; i < ilimit; ++i) {
109 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
112 work[j] += coeff[i+inchan*j];
115 for (j = 0; j < outchan; ++j) {
117 vals[x].channel[j] = 0;
118 else if (work[j] >= 1)
119 vals[x].channel[j] = 1;
121 vals[x].channel[j] = work[j];
124 i_plinf(im, 0, src->xsize, y, vals);
136 if (im->channels != outchan || im->xsize != src->xsize
137 || im->ysize != src->ysize
138 || i_maxcolors(im) < i_colorcount(src)) {
140 i_img_pal_new_low(im, src->xsize, src->ysize, outchan,
143 /* just translate the color table */
144 count = i_colorcount(src);
145 outcount = i_colorcount(im);
146 colors = mymalloc(count * sizeof(i_color));
147 i_getcolors(src, 0, colors, count);
148 for (index = 0; index < count; ++index) {
149 for (j = 0; j < outchan; ++j) {
151 for (i = 0; i < ilimit; ++i) {
152 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
155 work[j] += coeff[i+inchan*j] * 255.9;
158 for (j = 0; j < outchan; ++j) {
160 colors[index].channel[j] = 0;
161 else if (work[j] >= 255)
162 colors[index].channel[j] = 255;
164 colors[index].channel[j] = work[j];
167 if (count < outcount) {
168 i_setcolors(im, 0, colors, count);
171 i_setcolors(im, 0, colors, outcount);
172 i_addcolors(im, colors, count-outcount);
174 /* and copy the indicies */
175 vals = mymalloc(sizeof(i_palidx) * im->xsize);
176 for (y = 0; y < im->ysize; ++y) {
177 i_gpal(src, 0, im->xsize, y, vals);
178 i_ppal(im, 0, im->xsize, y, vals);
194 Tony Cook <tony@develop-help.com>