blob: a17f801dfaa0482098f71138281996816fc92cdd [file] [log] [blame]
Talustus3019a912013-04-06 11:50:07 +02001/*
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
30void 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 */
47static int fb_get_format(const struct fb *fb)
48{
49 int ao = fb->alpha_offset;
50 int ro = fb->red_offset;
Talustus3019a912013-04-06 11:50:07 +020051 int bo = fb->blue_offset;
52
53#define FB_FORMAT_UNKNOWN 0
54#define FB_FORMAT_RGB565 1
55#define FB_FORMAT_ARGB8888 2
56#define FB_FORMAT_RGBA8888 3
57#define FB_FORMAT_ABGR8888 4
58#define FB_FORMAT_BGRA8888 5
59
60 /* TODO: use offset */
61 if (fb->bpp == 16)
62 return FB_FORMAT_RGB565;
63
64 /* TODO: validate */
65 if (ao == 0 && ro == 8)
66 return FB_FORMAT_ARGB8888;
67
68 if (ao == 0 && bo == 8)
69 return FB_FORMAT_ABGR8888;
70
71 if (ro == 0)
72 return FB_FORMAT_RGBA8888;
73
74 if (bo == 0)
75 return FB_FORMAT_BGRA8888;
76
77 /* fallback */
78 return FB_FORMAT_UNKNOWN;
79}
80
81int fb_save_png(const struct fb *fb, const char *path)
82{
83 char *rgb_matrix;
84 int ret = -1;
85
86 /* Allocate RGB Matrix. */
87 rgb_matrix = malloc(fb->width * fb->height * 3);
88 if(!rgb_matrix) {
89 free(rgb_matrix);
90 return -1;
91 }
92
93 int fmt = fb_get_format(fb);
94 D("Framebuffer Pixel Format: %d", fmt);
95
96 switch(fmt) {
97 case FB_FORMAT_RGB565:
98 /* emulator use rgb565 */
99 ret = rgb565_to_rgb888(fb->data,
100 rgb_matrix, fb->width * fb->height);
101 break;
102 case FB_FORMAT_ARGB8888:
103 /* most devices use argb8888 */
104 ret = argb8888_to_rgb888(fb->data,
105 rgb_matrix, fb->width * fb->height);
106 break;
107 case FB_FORMAT_ABGR8888:
108 ret = abgr8888_to_rgb888(fb->data,
109 rgb_matrix, fb->width * fb->height);
110 break;
111 case FB_FORMAT_BGRA8888:
112 ret = bgra8888_to_rgb888(fb->data,
113 rgb_matrix, fb->width * fb->height);
114 break;
115 case FB_FORMAT_RGBA8888:
116 ret = rgba8888_to_rgb888(fb->data,
117 rgb_matrix, fb->width * fb->height);
118 break;
119 default:
120 D("Unsupported framebuffer type.");
121 break;
122 }
123
124 if (ret != 0)
125 D("Error while processing input image.");
126 else if (0 != (ret = save_png(path, rgb_matrix, fb->width, fb->height)))
127 D("Failed to save in PNG format.");
128
129 free(rgb_matrix);
130 return ret;
131}