blob: 5beb6a6d985fb86f9c50497d3c1cb60368dcbfda [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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 <stdlib.h>
18#include <unistd.h>
19
20#include <fcntl.h>
21#include <stdio.h>
22
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <sys/types.h>
26
27#include <linux/fb.h>
28#include <linux/kd.h>
29
30#include <pixelflinger/pixelflinger.h>
31
32#include "minui.h"
33
34// File signature for BMP files.
35// The letters 'BM' as a little-endian unsigned short.
36
37#define BMP_SIGNATURE 0x4d42
38
39typedef struct {
40 // constant, value should equal BMP_SIGNATURE
41 unsigned short bfType;
42 // size of the file in bytes.
43 unsigned long bfSize;
44 // must always be set to zero.
45 unsigned short bfReserved1;
46 // must always be set to zero.
47 unsigned short bfReserved2;
48 // offset from the beginning of the file to the bitmap data.
49 unsigned long bfOffBits;
50
51 // The BITMAPINFOHEADER:
52 // size of the BITMAPINFOHEADER structure, in bytes.
53 unsigned long biSize;
54 // width of the image, in pixels.
55 unsigned long biWidth;
56 // height of the image, in pixels.
57 unsigned long biHeight;
58 // number of planes of the target device, must be set to 1.
59 unsigned short biPlanes;
60 // number of bits per pixel.
61 unsigned short biBitCount;
62 // type of compression, zero means no compression.
63 unsigned long biCompression;
64 // size of the image data, in bytes. If there is no compression,
65 // it is valid to set this member to zero.
66 unsigned long biSizeImage;
67 // horizontal pixels per meter on the designated targer device,
68 // usually set to zero.
69 unsigned long biXPelsPerMeter;
70 // vertical pixels per meter on the designated targer device,
71 // usually set to zero.
72 unsigned long biYPelsPerMeter;
73 // number of colors used in the bitmap, if set to zero the
74 // number of colors is calculated using the biBitCount member.
75 unsigned long biClrUsed;
76 // number of color that are 'important' for the bitmap,
77 // if set to zero, all colors are important.
78 unsigned long biClrImportant;
79} __attribute__((packed)) BitMapFileHeader;
80
81int res_create_surface(const char* name, gr_surface* pSurface) {
82 char resPath[256];
83 BitMapFileHeader header;
84 GGLSurface* surface = NULL;
85 int result = 0;
86
87 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.bmp", name);
88 resPath[sizeof(resPath)-1] = '\0';
89 int fd = open(resPath, O_RDONLY);
90 if (fd == -1) {
91 result = -1;
92 goto exit;
93 }
94 size_t bytesRead = read(fd, &header, sizeof(header));
95 if (bytesRead != sizeof(header)) {
96 result = -2;
97 goto exit;
98 }
99 if (header.bfType != BMP_SIGNATURE) {
100 result = -3; // Not a legal header
101 goto exit;
102 }
103 if (header.biPlanes != 1) {
104 result = -4;
105 goto exit;
106 }
107 if (!(header.biBitCount == 24 || header.biBitCount == 32)) {
108 result = -5;
109 goto exit;
110 }
111 if (header.biCompression != 0) {
112 result = -6;
113 goto exit;
114 }
115 size_t width = header.biWidth;
116 size_t height = header.biHeight;
117 size_t stride = 4 * width;
118 size_t pixelSize = stride * height;
119
120 surface = malloc(sizeof(GGLSurface) + pixelSize);
121 if (surface == NULL) {
122 result = -7;
123 goto exit;
124 }
125 unsigned char* pData = (unsigned char*) (surface + 1);
126 surface->version = sizeof(GGLSurface);
127 surface->width = width;
128 surface->height = height;
129 surface->stride = width; /* Yes, pixels, not bytes */
130 surface->data = pData;
131 surface->format = (header.biBitCount == 24) ?
132 GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;
133
134 // Source pixel bytes are stored B G R {A}
135
136 lseek(fd, header.bfOffBits, SEEK_SET);
137 size_t y;
138 if (header.biBitCount == 24) { // RGB
139 size_t inputStride = (((3 * width + 3) >> 2) << 2);
140 for (y = 0; y < height; y++) {
141 unsigned char* pRow = pData + (height - (y + 1)) * stride;
142 bytesRead = read(fd, pRow, inputStride);
143 if (bytesRead != inputStride) {
144 result = -8;
145 goto exit;
146 }
147 int x;
148 for(x = width - 1; x >= 0; x--) {
149 int sx = x * 3;
150 int dx = x * 4;
151 unsigned char b = pRow[sx];
152 unsigned char g = pRow[sx + 1];
153 unsigned char r = pRow[sx + 2];
154 unsigned char a = 0xff;
155 pRow[dx ] = r; // r
156 pRow[dx + 1] = g; // g
157 pRow[dx + 2] = b; // b;
158 pRow[dx + 3] = a;
159 }
160 }
161 } else { // RGBA
162 for (y = 0; y < height; y++) {
163 unsigned char* pRow = pData + (height - (y + 1)) * stride;
164 bytesRead = read(fd, pRow, stride);
165 if (bytesRead != stride) {
166 result = -9;
167 goto exit;
168 }
169 size_t x;
170 for(x = 0; x < width; x++) {
171 size_t xx = x * 4;
172 unsigned char b = pRow[xx];
173 unsigned char g = pRow[xx + 1];
174 unsigned char r = pRow[xx + 2];
175 unsigned char a = pRow[xx + 3];
176 pRow[xx ] = r;
177 pRow[xx + 1] = g;
178 pRow[xx + 2] = b;
179 pRow[xx + 3] = a;
180 }
181 }
182 }
183 *pSurface = (gr_surface) surface;
184
185exit:
186 if (fd >= 0) {
187 close(fd);
188 }
189 if (result < 0) {
190 if (surface) {
191 free(surface);
192 }
193 }
194 return result;
195}
196
197void res_free_surface(gr_surface surface) {
198 GGLSurface* pSurface = (GGLSurface*) surface;
199 if (pSurface) {
200 free(pSurface);
201 }
202}