summaryrefslogtreecommitdiffstats
path: root/apps/recorder/bmp.h
blob: 1f29103556aa7eccfe7a3faceda3bb188bb55fc5 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Daniel Stenberg
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef _BMP_H_
#define _BMP_H_

#include "config.h"
#include "lcd.h"
#include "inttypes.h"
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif

#define ARRAY_SIZE(array)    (int)(sizeof(array)/(sizeof(array[0])))

#define IMG_NORESIZE           0
#define IMG_RESIZE             1
#if LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
#define MAX_WIDTH 8 
#else
#define MAX_WIDTH LCD_WIDTH
#endif

struct uint8_rgb {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
};

struct dim {
    short width;
    short height;
};

struct rowset {
    short rowstep;
    short rowstart;
    short rowstop;
};

#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
extern const unsigned char dither_matrix[16][16];
static inline unsigned char dither_mat(unsigned int x, unsigned int y)
{
    return dither_matrix[y][x];
}
#endif

static inline unsigned brightness(struct uint8_rgb color)
{
    return (3 * (unsigned)color.red + 6 * (unsigned)color.green
              + (unsigned)color.blue) / 10;
}

#if ((LCD_DEPTH == 2) && (LCD_PIXELFORMAT == VERTICAL_INTERLEAVED)) \
 || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH == 2) \
     && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED))
extern const unsigned short vi_pattern[4];
static inline unsigned short vi_pat(unsigned int bright)
{
    return vi_pattern[bright];
}
#endif

static inline int get_fb_height(struct bitmap *bm, bool remote)
{
    const int height = bm->height;
#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
    const int format = bm->format;
#endif
    int dst_height;

#if !defined(HAVE_REMOTE_LCD) || \
    (defined(HAVE_REMOTE_LCD) &&(LCD_REMOTE_DEPTH == 1))
    (void) remote;
#endif

#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
    if (format == FORMAT_NATIVE) {
#if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
        if (remote) {
#if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
            dst_height = (height + 7) >> 3;
#endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
        } else
#endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */
        {
#if LCD_DEPTH == 2
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
            dst_height = height;
#elif LCD_PIXELFORMAT == VERTICAL_PACKING
            dst_height = (height + 3) >> 2;
#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
            dst_height = (height + 7) >> 3;
#endif /* LCD_PIXELFORMAT */
#elif LCD_DEPTH == 16
            dst_height = height;
#endif /* LCD_DEPTH */
        }
    } else
#endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
    {
        dst_height = (height + 7) >> 3;
    }

    return dst_height;
}

static inline int get_fb_width(struct bitmap *bm, bool remote)
{
    const int width = bm->width;
#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
    const int format = bm->format;
#endif
    int dst_width;

#if !defined(HAVE_REMOTE_LCD) || \
    (defined(HAVE_REMOTE_LCD) &&(LCD_REMOTE_DEPTH == 1))
    (void) remote;
#endif

#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
    if (format == FORMAT_NATIVE) {
#if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
        if (remote) {
#if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
            dst_width  = width;
#endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
        } else
#endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */
        {
#if LCD_DEPTH == 2
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
            dst_width  = (width + 3) >> 2;
#elif LCD_PIXELFORMAT == VERTICAL_PACKING
            dst_width  = width;
#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
            dst_width  = width;
#endif /* LCD_PIXELFORMAT */
#elif LCD_DEPTH == 16
            dst_width  = width;
#endif /* LCD_DEPTH */
        }
    } else
#endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
    {
        dst_width  = width;
    }

    return dst_width;
}

static inline int get_totalsize(struct bitmap *bm, bool remote)
{
    int sz;
#if defined(HAVE_REMOTE_LCD) && \
    (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
    if (remote)
        sz = sizeof(fb_remote_data);
    else
#endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
        sz = sizeof(fb_data);

    return get_fb_width(bm, remote) * get_fb_height(bm, remote) * sz;
}

/*********************************************************************
 * read_bmp_file()
 *
 * Reads a 8bit BMP file and puts the data in a 1-pixel-per-byte
 * array.
 * Returns < 0 for error, or number of bytes used from the bitmap buffer
 *
 **********************************************/
int read_bmp_file(const char* filename,
                  struct bitmap *bm,
                  int maxsize,
                  int format);

int read_bmp_fd(int fd,
                struct bitmap *bm,
                int maxsize,
                int format);
#endif