summaryrefslogtreecommitdiffstats
path: root/minui/resources.c
blob: 5beb6a6d985fb86f9c50497d3c1cb60368dcbfda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <stdio.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>

#include <linux/fb.h>
#include <linux/kd.h>

#include <pixelflinger/pixelflinger.h>

#include "minui.h"

// File signature for BMP files.
// The letters 'BM' as a little-endian unsigned short.

#define BMP_SIGNATURE 0x4d42

typedef struct {
    // constant, value should equal BMP_SIGNATURE
    unsigned short  bfType;
    // size of the file in bytes.
    unsigned long   bfSize;
    // must always be set to zero.
    unsigned short  bfReserved1;
    // must always be set to zero.
    unsigned short  bfReserved2;
    // offset from the beginning of the file to the bitmap data.
    unsigned long   bfOffBits;

    // The BITMAPINFOHEADER:
    // size of the BITMAPINFOHEADER structure, in bytes.
    unsigned long   biSize;
    // width of the image, in pixels.
    unsigned long   biWidth;
    // height of the image, in pixels.
    unsigned long   biHeight;
    // number of planes of the target device, must be set to 1.
    unsigned short  biPlanes;
    // number of bits per pixel.
    unsigned short  biBitCount;
    // type of compression, zero means no compression.
    unsigned long   biCompression;
    // size of the image data, in bytes. If there is no compression,
    // it is valid to set this member to zero.
    unsigned long   biSizeImage;
    // horizontal pixels per meter on the designated targer device,
    // usually set to zero.
    unsigned long   biXPelsPerMeter;
    // vertical pixels per meter on the designated targer device,
    // usually set to zero.
    unsigned long   biYPelsPerMeter;
    // number of colors used in the bitmap, if set to zero the
    // number of colors is calculated using the biBitCount member.
    unsigned long   biClrUsed;
    // number of color that are 'important' for the bitmap,
    // if set to zero, all colors are important.
    unsigned long   biClrImportant;
} __attribute__((packed)) BitMapFileHeader;

int res_create_surface(const char* name, gr_surface* pSurface) {
    char resPath[256];
    BitMapFileHeader header;
    GGLSurface* surface = NULL;
    int result = 0;
    
    snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.bmp", name);
    resPath[sizeof(resPath)-1] = '\0';
    int fd = open(resPath, O_RDONLY);
    if (fd == -1) {
        result = -1;
        goto exit;
    }
    size_t bytesRead = read(fd, &header, sizeof(header));
    if (bytesRead != sizeof(header)) {
        result = -2;
        goto exit;
    }
    if (header.bfType != BMP_SIGNATURE) {
        result = -3; // Not a legal header
        goto exit;
    }
    if (header.biPlanes != 1) {
        result = -4;
        goto exit;
    }
    if (!(header.biBitCount == 24 || header.biBitCount == 32)) {
        result = -5;
        goto exit;
    }
    if (header.biCompression != 0) {
        result = -6;
        goto exit;
    }
    size_t width = header.biWidth;
    size_t height = header.biHeight;
    size_t stride = 4 * width;
    size_t pixelSize = stride * height;
    
    surface = malloc(sizeof(GGLSurface) + pixelSize);
    if (surface == NULL) {
        result = -7;
        goto exit;
    }
    unsigned char* pData = (unsigned char*) (surface + 1);
    surface->version = sizeof(GGLSurface);
    surface->width = width;
    surface->height = height;
    surface->stride = width; /* Yes, pixels, not bytes */
    surface->data = pData;
    surface->format = (header.biBitCount == 24) ?
            GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;

    // Source pixel bytes are stored B G R {A}
    
    lseek(fd, header.bfOffBits, SEEK_SET);
    size_t y;
    if (header.biBitCount == 24) { // RGB
        size_t inputStride = (((3 * width + 3) >> 2) << 2);
        for (y = 0; y < height; y++) {
            unsigned char* pRow = pData + (height - (y + 1)) * stride;
            bytesRead = read(fd,  pRow, inputStride);
            if (bytesRead != inputStride) {
                result = -8;
                goto exit;
            }
            int x;
            for(x = width - 1; x >= 0; x--) {
                int sx = x * 3;
                int dx = x * 4;
                unsigned char b = pRow[sx];
                unsigned char g = pRow[sx + 1];
                unsigned char r = pRow[sx + 2];
                unsigned char a = 0xff;
                pRow[dx    ] = r; // r
                pRow[dx + 1] = g; // g
                pRow[dx + 2] = b; // b;
                pRow[dx + 3] = a;
            }
        }
    } else { // RGBA
        for (y = 0; y < height; y++) {
            unsigned char* pRow = pData + (height - (y + 1)) * stride;
            bytesRead = read(fd,  pRow, stride);
            if (bytesRead != stride) {
                result = -9;
                goto exit;
            }
            size_t x;
            for(x = 0; x < width; x++) {
                size_t xx = x * 4;
                unsigned char b = pRow[xx];
                unsigned char g = pRow[xx + 1];
                unsigned char r = pRow[xx + 2];
                unsigned char a = pRow[xx + 3];
                pRow[xx    ] = r;
                pRow[xx + 1] = g;
                pRow[xx + 2] = b;
                pRow[xx + 3] = a;
            }
        }
    }
    *pSurface = (gr_surface) surface;

exit:
    if (fd >= 0) {
        close(fd);
    }
    if (result < 0) {
        if (surface) {
            free(surface);
        }
    }
    return result;
}

void res_free_surface(gr_surface surface) {
    GGLSurface* pSurface = (GGLSurface*) surface;
    if (pSurface) {
        free(pSurface);
    }
}