2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #define to565(r,g,b) \
23 ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
25 #define from565_r(x) ((((x) >> 11) & 0x1f) * 255 / 31)
26 #define from565_g(x) ((((x) >> 5) & 0x3f) * 255 / 63)
27 #define from565_b(x) (((x) & 0x1f) * 255 / 31)
34 while(read(0, in, 3) == 3) {
35 out = to565(in[0],in[1],in[2]);
41 void to_565_raw_dither(int width)
48 int* error = malloc((width+2) * 3 * sizeof(int));
49 int* next_error = malloc((width+2) * 3 * sizeof(int));
50 memset(error, 0, (width+2) * 3 * sizeof(int));
51 memset(next_error, 0, (width+2) * 3 * sizeof(int));
52 error += 3; // array goes from [-3..((width+1)*3+2)]
55 while(read(0, in, 3) == 3) {
56 int r = in[0] + error[i*3+0];
57 int rb = (r < 0) ? 0 : ((r > 255) ? 255 : r);
59 int g = in[1] + error[i*3+1];
60 int gb = (g < 0) ? 0 : ((g > 255) ? 255 : g);
62 int b = in[2] + error[i*3+2];
63 int bb = (b < 0) ? 0 : ((b > 255) ? 255 : b);
65 out = to565(rb, gb, bb);
68 #define apply_error(ch) { \
69 next_error[(i-1)*3+ch] += e * 3 / 16; \
70 next_error[(i)*3+ch] += e * 5 / 16; \
71 next_error[(i+1)*3+ch] += e * 1 / 16; \
72 error[(i+1)*3+ch] += e - ((e*1/16) + (e*3/16) + (e*5/16)); \
75 e = r - from565_r(out);
78 e = g - from565_g(out);
81 e = b - from565_b(out);
88 // error <- next_error; next_error <- 0
89 int* temp = error; error = next_error; next_error = temp;
90 memset(next_error, 0, (width+1) * 3 * sizeof(int));
101 void to_565_rle(void)
104 unsigned short last, color, count;
108 while(read(0, in, 3) == 3) {
109 color = to565(in[0],in[1],in[2]);
111 if ((color == last) && (count != 65535)) {
128 fprintf(stderr,"%d pixels\n",total);
131 int main(int argc, char **argv)
133 if ((argc == 2) && (!strcmp(argv[1],"-rle"))) {
136 if (argc > 2 && (!strcmp(argv[1], "-w"))) {
137 to_565_raw_dither(atoi(argv[2]));