Commit | Line | Data |
---|---|---|
f5991c03 TC |
1 | /* |
2 | =head1 NAME | |
3 | ||
62869327 | 4 | convert.im - image conversions |
f5991c03 TC |
5 | |
6 | =head1 SYNOPSIS | |
7 | ||
8 | i_convert(outimage, srcimage, coeff, outchans, inchans) | |
9 | ||
10 | =head1 DESCRIPTION | |
11 | ||
12 | Converts images from one format to another, typically in this case for | |
13 | converting from RGBA to greyscale and back. | |
14 | ||
15 | =over | |
16 | ||
17 | =cut | |
18 | */ | |
19 | ||
92bda632 | 20 | #include "imager.h" |
f5991c03 TC |
21 | |
22 | ||
23 | /* | |
d5477d3d | 24 | =item i_convert(src, coeff, outchan, inchan) |
f5991c03 TC |
25 | |
26 | Converts the image src into another image. | |
27 | ||
28 | coeff contains the co-efficients of an outchan x inchan matrix, for | |
29 | each output pixel: | |
30 | ||
31 | coeff[0], coeff[1] ... | |
32 | im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1] | |
33 | ... coeff[inchan*outchan-1] | |
34 | ||
35 | If im has the wrong number of channels or is the wrong size then | |
36 | i_convert() will re-create it. | |
37 | ||
faa9b3e7 TC |
38 | Now handles images with more than 8-bits/sample. |
39 | ||
f5991c03 TC |
40 | =cut |
41 | */ | |
42 | ||
d5477d3d | 43 | i_img * |
62869327 | 44 | i_convert(i_img *src, const double *coeff, int outchan, int inchan) { |
f5991c03 TC |
45 | int x, y; |
46 | int i, j; | |
a743c0a6 | 47 | int ilimit; |
f5991c03 | 48 | double work[MAXCHANNELS]; |
d5477d3d | 49 | i_img *im = NULL; |
f5991c03 | 50 | |
d5477d3d | 51 | mm_log((1,"i_convert(src %p, coeff %p,outchan %d, inchan %d)\n",im,src, coeff,outchan, inchan)); |
f5991c03 TC |
52 | |
53 | i_clear_error(); | |
54 | ||
55 | ilimit = inchan; | |
56 | if (ilimit > src->channels) | |
57 | ilimit = src->channels; | |
58 | if (outchan > MAXCHANNELS) { | |
59 | i_push_error(0, "cannot have outchan > MAXCHANNELS"); | |
60 | return 0; | |
61 | } | |
62 | ||
d5477d3d TC |
63 | if (src->type == i_direct_type) { |
64 | im = i_sametype_chans(src, src->xsize, src->ysize, outchan); | |
62869327 TC |
65 | #code src->bits <= i_8_bits |
66 | IM_COLOR *vals; | |
67 | ||
68 | /* we can always allocate a single scanline of i_color */ | |
69 | vals = mymalloc(sizeof(IM_COLOR) * src->xsize); /* checked 04Jul05 tonyc */ | |
70 | for (y = 0; y < src->ysize; ++y) { | |
71 | IM_GLIN(src, 0, src->xsize, y, vals); | |
72 | for (x = 0; x < src->xsize; ++x) { | |
73 | for (j = 0; j < outchan; ++j) { | |
74 | work[j] = 0; | |
75 | for (i = 0; i < ilimit; ++i) { | |
76 | work[j] += coeff[i+inchan*j] * vals[x].channel[i]; | |
77 | } | |
78 | if (i < inchan) { | |
79 | work[j] += coeff[i+inchan*j] * IM_SAMPLE_MAX; | |
80 | } | |
81 | } | |
82 | for (j = 0; j < outchan; ++j) { | |
83 | if (work[j] < 0) | |
84 | vals[x].channel[j] = 0; | |
85 | else if (work[j] >= IM_SAMPLE_MAX) | |
86 | vals[x].channel[j] = IM_SAMPLE_MAX; | |
87 | else | |
88 | vals[x].channel[j] = work[j]; | |
89 | } | |
faa9b3e7 | 90 | } |
62869327 | 91 | IM_PLIN(im, 0, src->xsize, y, vals); |
faa9b3e7 | 92 | } |
62869327 TC |
93 | myfree(vals); |
94 | #/code | |
f5991c03 | 95 | } |
faa9b3e7 TC |
96 | else { |
97 | int count; | |
98 | int outcount; | |
99 | int index; | |
100 | i_color *colors; | |
101 | i_palidx *vals; | |
102 | ||
d5477d3d TC |
103 | im = i_img_pal_new(src->xsize, src->ysize, outchan, |
104 | i_maxcolors(src)); | |
105 | ||
faa9b3e7 TC |
106 | /* just translate the color table */ |
107 | count = i_colorcount(src); | |
108 | outcount = i_colorcount(im); | |
f0960b14 TC |
109 | /* color table allocated for image, so it must fit */ |
110 | colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */ | |
faa9b3e7 TC |
111 | i_getcolors(src, 0, colors, count); |
112 | for (index = 0; index < count; ++index) { | |
f5991c03 | 113 | for (j = 0; j < outchan; ++j) { |
faa9b3e7 TC |
114 | work[j] = 0; |
115 | for (i = 0; i < ilimit; ++i) { | |
116 | work[j] += coeff[i+inchan*j] * colors[index].channel[i]; | |
117 | } | |
118 | if (i < inchan) { | |
119 | work[j] += coeff[i+inchan*j] * 255.9; | |
120 | } | |
f5991c03 TC |
121 | } |
122 | for (j = 0; j < outchan; ++j) { | |
faa9b3e7 TC |
123 | if (work[j] < 0) |
124 | colors[index].channel[j] = 0; | |
125 | else if (work[j] >= 255) | |
126 | colors[index].channel[j] = 255; | |
127 | else | |
128 | colors[index].channel[j] = work[j]; | |
f5991c03 TC |
129 | } |
130 | } | |
faa9b3e7 TC |
131 | if (count < outcount) { |
132 | i_setcolors(im, 0, colors, count); | |
133 | } | |
134 | else { | |
135 | i_setcolors(im, 0, colors, outcount); | |
136 | i_addcolors(im, colors, count-outcount); | |
137 | } | |
138 | /* and copy the indicies */ | |
f0960b14 TC |
139 | /* i_palidx is always unsigned char and will never be bigger than short |
140 | and since a line of 4-byte i_colors can fit then a line of i_palidx | |
141 | will fit */ | |
142 | vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */ | |
faa9b3e7 TC |
143 | for (y = 0; y < im->ysize; ++y) { |
144 | i_gpal(src, 0, im->xsize, y, vals); | |
145 | i_ppal(im, 0, im->xsize, y, vals); | |
146 | } | |
a73aeb5f AMH |
147 | myfree(vals); |
148 | myfree(colors); | |
f5991c03 | 149 | } |
faa9b3e7 | 150 | |
d5477d3d | 151 | return im; |
f5991c03 TC |
152 | } |
153 | ||
154 | /* | |
155 | =back | |
156 | ||
157 | =head1 SEE ALSO | |
158 | ||
159 | Imager(3) | |
160 | ||
161 | =head1 AUTHOR | |
162 | ||
163 | Tony Cook <tony@develop-help.com> | |
164 | ||
165 | =cut | |
166 | */ |