Talustus | 3019a91 | 2013-04-06 11:50:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * -- http://android-fb2png.googlecode.com/svn/trunk/fb.c -- |
| 3 | * |
| 4 | * Copyright 2011, Kyan He <kyan.ql.he@gmail.com> |
| 5 | * |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "fb.h" |
| 28 | #include "img_process.h" |
| 29 | |
| 30 | void fb_dump(const struct fb* fb) |
| 31 | { |
| 32 | D("%12s : %d", "bpp", fb->bpp); |
| 33 | D("%12s : %d", "size", fb->size); |
| 34 | D("%12s : %d", "width", fb->width); |
| 35 | D("%12s : %d", "height", fb->height); |
| 36 | D("%12s : %d %d %d %d", "ARGB offset", |
| 37 | fb->alpha_offset, fb->red_offset, |
| 38 | fb->green_offset, fb->blue_offset); |
| 39 | D("%12s : %d %d %d %d", "ARGB length", |
| 40 | fb->alpha_length, fb->red_length, |
| 41 | fb->green_length, fb->blue_length); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the format of fb. |
| 46 | */ |
| 47 | static int fb_get_format(const struct fb *fb) |
| 48 | { |
| 49 | int ao = fb->alpha_offset; |
| 50 | int ro = fb->red_offset; |
| 51 | int go = fb->green_offset; |
| 52 | int bo = fb->blue_offset; |
| 53 | |
| 54 | #define FB_FORMAT_UNKNOWN 0 |
| 55 | #define FB_FORMAT_RGB565 1 |
| 56 | #define FB_FORMAT_ARGB8888 2 |
| 57 | #define FB_FORMAT_RGBA8888 3 |
| 58 | #define FB_FORMAT_ABGR8888 4 |
| 59 | #define FB_FORMAT_BGRA8888 5 |
| 60 | |
| 61 | /* TODO: use offset */ |
| 62 | if (fb->bpp == 16) |
| 63 | return FB_FORMAT_RGB565; |
| 64 | |
| 65 | /* TODO: validate */ |
| 66 | if (ao == 0 && ro == 8) |
| 67 | return FB_FORMAT_ARGB8888; |
| 68 | |
| 69 | if (ao == 0 && bo == 8) |
| 70 | return FB_FORMAT_ABGR8888; |
| 71 | |
| 72 | if (ro == 0) |
| 73 | return FB_FORMAT_RGBA8888; |
| 74 | |
| 75 | if (bo == 0) |
| 76 | return FB_FORMAT_BGRA8888; |
| 77 | |
| 78 | /* fallback */ |
| 79 | return FB_FORMAT_UNKNOWN; |
| 80 | } |
| 81 | |
| 82 | int fb_save_png(const struct fb *fb, const char *path) |
| 83 | { |
| 84 | char *rgb_matrix; |
| 85 | int ret = -1; |
| 86 | |
| 87 | /* Allocate RGB Matrix. */ |
| 88 | rgb_matrix = malloc(fb->width * fb->height * 3); |
| 89 | if(!rgb_matrix) { |
| 90 | free(rgb_matrix); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | int fmt = fb_get_format(fb); |
| 95 | D("Framebuffer Pixel Format: %d", fmt); |
| 96 | |
| 97 | switch(fmt) { |
| 98 | case FB_FORMAT_RGB565: |
| 99 | /* emulator use rgb565 */ |
| 100 | ret = rgb565_to_rgb888(fb->data, |
| 101 | rgb_matrix, fb->width * fb->height); |
| 102 | break; |
| 103 | case FB_FORMAT_ARGB8888: |
| 104 | /* most devices use argb8888 */ |
| 105 | ret = argb8888_to_rgb888(fb->data, |
| 106 | rgb_matrix, fb->width * fb->height); |
| 107 | break; |
| 108 | case FB_FORMAT_ABGR8888: |
| 109 | ret = abgr8888_to_rgb888(fb->data, |
| 110 | rgb_matrix, fb->width * fb->height); |
| 111 | break; |
| 112 | case FB_FORMAT_BGRA8888: |
| 113 | ret = bgra8888_to_rgb888(fb->data, |
| 114 | rgb_matrix, fb->width * fb->height); |
| 115 | break; |
| 116 | case FB_FORMAT_RGBA8888: |
| 117 | ret = rgba8888_to_rgb888(fb->data, |
| 118 | rgb_matrix, fb->width * fb->height); |
| 119 | break; |
| 120 | default: |
| 121 | D("Unsupported framebuffer type."); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (ret != 0) |
| 126 | D("Error while processing input image."); |
| 127 | else if (0 != (ret = save_png(path, rgb_matrix, fb->width, fb->height))) |
| 128 | D("Failed to save in PNG format."); |
| 129 | |
| 130 | free(rgb_matrix); |
| 131 | return ret; |
| 132 | } |