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) {
48 double work[MAXCHANNELS];
50 mm_log((1,"i_convert(im %p, src, %p, coeff %p,outchan %d, inchan %d)\n",im,src, coeff,outchan, inchan));
55 if (ilimit > src->channels)
56 ilimit = src->channels;
57 if (outchan > MAXCHANNELS) {
58 i_push_error(0, "cannot have outchan > MAXCHANNELS");
62 if (im->type == i_direct_type || src->type == i_direct_type) {
63 /* first check the output image */
64 if (im->channels != outchan || im->xsize != src->xsize
65 || im->ysize != src->ysize) {
67 i_img_empty_ch(im, src->xsize, src->ysize, outchan);
69 if (im->bits == i_8_bits && src->bits == i_8_bits) {
72 vals = mymalloc(sizeof(i_color) * src->xsize);
73 for (y = 0; y < src->ysize; ++y) {
74 i_glin(src, 0, src->xsize, y, vals);
75 for (x = 0; x < src->xsize; ++x) {
76 for (j = 0; j < outchan; ++j) {
78 for (i = 0; i < ilimit; ++i) {
79 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
82 work[j] += coeff[i+inchan*j] * 255.9;
85 for (j = 0; j < outchan; ++j) {
87 vals[x].channel[j] = 0;
88 else if (work[j] >= 256)
89 vals[x].channel[j] = 255;
91 vals[x].channel[j] = work[j];
94 i_plin(im, 0, src->xsize, y, vals);
101 vals = mymalloc(sizeof(i_fcolor) * src->xsize);
102 for (y = 0; y < src->ysize; ++y) {
103 i_glinf(src, 0, src->xsize, y, vals);
104 for (x = 0; x < src->xsize; ++x) {
105 for (j = 0; j < outchan; ++j) {
107 for (i = 0; i < ilimit; ++i) {
108 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
111 work[j] += coeff[i+inchan*j];
114 for (j = 0; j < outchan; ++j) {
116 vals[x].channel[j] = 0;
117 else if (work[j] >= 1)
118 vals[x].channel[j] = 1;
120 vals[x].channel[j] = work[j];
123 i_plinf(im, 0, src->xsize, y, vals);
135 if (im->channels != outchan || im->xsize != src->xsize
136 || im->ysize != src->ysize
137 || i_maxcolors(im) < i_colorcount(src)) {
139 i_img_pal_new_low(im, src->xsize, src->ysize, outchan,
142 /* just translate the color table */
143 count = i_colorcount(src);
144 outcount = i_colorcount(im);
145 colors = mymalloc(count * sizeof(i_color));
146 i_getcolors(src, 0, colors, count);
147 for (index = 0; index < count; ++index) {
148 for (j = 0; j < outchan; ++j) {
150 for (i = 0; i < ilimit; ++i) {
151 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
154 work[j] += coeff[i+inchan*j] * 255.9;
157 for (j = 0; j < outchan; ++j) {
159 colors[index].channel[j] = 0;
160 else if (work[j] >= 255)
161 colors[index].channel[j] = 255;
163 colors[index].channel[j] = work[j];
166 if (count < outcount) {
167 i_setcolors(im, 0, colors, count);
170 i_setcolors(im, 0, colors, outcount);
171 i_addcolors(im, colors, count-outcount);
173 /* and copy the indicies */
174 vals = mymalloc(sizeof(i_palidx) * im->xsize);
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);
195 Tony Cook <tony@develop-help.com>