The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #define to565(r,g,b) \ |
| 23 | ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)) |
| 24 | |
| 25 | void to_565_raw(void) |
| 26 | { |
| 27 | unsigned char in[3]; |
| 28 | unsigned short out; |
| 29 | |
| 30 | while(read(0, in, 3) == 3) { |
| 31 | out = to565(in[0],in[1],in[2]); |
| 32 | write(1, &out, 2); |
| 33 | } |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | void to_565_rle(void) |
| 38 | { |
| 39 | unsigned char in[3]; |
| 40 | unsigned short last, color, count; |
| 41 | unsigned total = 0; |
| 42 | count = 0; |
| 43 | |
| 44 | while(read(0, in, 3) == 3) { |
| 45 | color = to565(in[0],in[1],in[2]); |
| 46 | if (count) { |
| 47 | if ((color == last) && (count != 65535)) { |
| 48 | count++; |
| 49 | continue; |
| 50 | } else { |
| 51 | write(1, &count, 2); |
| 52 | write(1, &last, 2); |
| 53 | total += count; |
| 54 | } |
| 55 | } |
| 56 | last = color; |
| 57 | count = 1; |
| 58 | } |
| 59 | if (count) { |
| 60 | write(1, &count, 2); |
| 61 | write(1, &last, 2); |
| 62 | total += count; |
| 63 | } |
| 64 | fprintf(stderr,"%d pixels\n",total); |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char **argv) |
| 68 | { |
| 69 | if ((argc > 1) && (!strcmp(argv[1],"-rle"))) { |
| 70 | to_565_rle(); |
| 71 | } else { |
| 72 | to_565_raw(); |
| 73 | } |
| 74 | return 0; |
| 75 | } |