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(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 *src, const float *coeff, int outchan, int inchan) {
48 double work[MAXCHANNELS];
51 mm_log((1,"i_convert(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 (src->type == i_direct_type) {
64 im = i_sametype_chans(src, src->xsize, src->ysize, outchan);
65 if (src->bits == i_8_bits) {
68 /* we can always allocate a single scanline of i_color */
69 vals = mymalloc(sizeof(i_color) * src->xsize); /* checked 04Jul05 tonyc */
70 for (y = 0; y < src->ysize; ++y) {
71 i_glin(src, 0, src->xsize, y, vals);
72 for (x = 0; x < src->xsize; ++x) {
73 for (j = 0; j < outchan; ++j) {
75 for (i = 0; i < ilimit; ++i) {
76 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
79 work[j] += coeff[i+inchan*j] * 255.9;
82 for (j = 0; j < outchan; ++j) {
84 vals[x].channel[j] = 0;
85 else if (work[j] >= 256)
86 vals[x].channel[j] = 255;
88 vals[x].channel[j] = work[j];
91 i_plin(im, 0, src->xsize, y, vals);
98 /* we can always allocate a single scanline of i_fcolor
100 vals = mymalloc(sizeof(i_fcolor) * src->xsize); /* checked 4Jul05 tonyc */
101 for (y = 0; y < src->ysize; ++y) {
102 i_glinf(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];
113 for (j = 0; j < outchan; ++j) {
115 vals[x].channel[j] = 0;
116 else if (work[j] >= 1)
117 vals[x].channel[j] = 1;
119 vals[x].channel[j] = work[j];
122 i_plinf(im, 0, src->xsize, y, vals);
134 im = i_img_pal_new(src->xsize, src->ysize, outchan,
137 /* just translate the color table */
138 count = i_colorcount(src);
139 outcount = i_colorcount(im);
140 /* color table allocated for image, so it must fit */
141 colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */
142 i_getcolors(src, 0, colors, count);
143 for (index = 0; index < count; ++index) {
144 for (j = 0; j < outchan; ++j) {
146 for (i = 0; i < ilimit; ++i) {
147 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
150 work[j] += coeff[i+inchan*j] * 255.9;
153 for (j = 0; j < outchan; ++j) {
155 colors[index].channel[j] = 0;
156 else if (work[j] >= 255)
157 colors[index].channel[j] = 255;
159 colors[index].channel[j] = work[j];
162 if (count < outcount) {
163 i_setcolors(im, 0, colors, count);
166 i_setcolors(im, 0, colors, outcount);
167 i_addcolors(im, colors, count-outcount);
169 /* and copy the indicies */
170 /* i_palidx is always unsigned char and will never be bigger than short
171 and since a line of 4-byte i_colors can fit then a line of i_palidx
173 vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */
174 for (y = 0; y < im->ysize; ++y) {
175 i_gpal(src, 0, im->xsize, y, vals);
176 i_ppal(im, 0, im->xsize, y, vals);
194 Tony Cook <tony@develop-help.com>