summaryrefslogtreecommitdiff
path: root/firmware/screendump.c
blob: 1876df8ac05247c1df244b86ff4c591fe8c85756 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/***************************************************************************
 *             __________               __   ___.                  
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \  
 *                     \/            \/     \/    \/            \/ 
 * $Id$
 *
 * Copyright (C) 2009 by Jens Arnold
 *
 * 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.
 *
 ****************************************************************************/

#include "config.h"
#include "screendump.h"

#include "file.h"
#include "general.h"
#include "lcd.h"
#include "stdlib.h"
#include "string.h"
#include "system.h"

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

#if LCD_DEPTH == 16
#define BMP_COMPRESSION 3 /* BI_BITFIELDS */
#define BMP_NUMCOLORS 3
#else /* LCD_DEPTH != 16 */
#define BMP_COMPRESSION 0 /* BI_RGB */
#if LCD_DEPTH <= 8
#ifdef HAVE_LCD_SPLIT
#define BMP_NUMCOLORS (2 << LCD_DEPTH)
#else
#define BMP_NUMCOLORS (1 << LCD_DEPTH)
#endif
#else /* LCD_DEPTH > 8 */
#define BMP_NUMCOLORS 0
#endif /* LCD_DEPTH > 8 */
#endif /* LCD_DEPTH != 16 */

#if LCD_DEPTH <= 4
#define BMP_BPP 4
#define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
#elif LCD_DEPTH <= 8
#define BMP_BPP 8
#define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
#elif LCD_DEPTH <= 16
#define BMP_BPP 16
#define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
#else
#define BMP_BPP 24
#define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
#endif

#define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
#define BMP_DATASIZE   (BMP_LINESIZE * (LCD_HEIGHT+LCD_SPLIT_LINES))
#define BMP_TOTALSIZE  (BMP_HEADERSIZE + BMP_DATASIZE)

static const unsigned char bmpheader[] =
{
    0x42, 0x4d,                 /* 'BM' */
    LE32_CONST(BMP_TOTALSIZE),  /* Total file size */
    0x00, 0x00, 0x00, 0x00,     /* Reserved */
    LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */

    0x28, 0x00, 0x00, 0x00,     /* Size of (2nd) header */
    LE32_CONST(LCD_WIDTH),      /* Width in pixels */
    LE32_CONST(LCD_HEIGHT+LCD_SPLIT_LINES),  /* Height in pixels */
    0x01, 0x00,                 /* Number of planes (always 1) */
    LE16_CONST(BMP_BPP),        /* Bits per pixel 1/4/8/16/24 */
    LE32_CONST(BMP_COMPRESSION),/* Compression mode */
    LE32_CONST(BMP_DATASIZE),   /* Size of bitmap data */
    0xc4, 0x0e, 0x00, 0x00,     /* Horizontal resolution (pixels/meter) */
    0xc4, 0x0e, 0x00, 0x00,     /* Vertical resolution (pixels/meter) */
    LE32_CONST(BMP_NUMCOLORS),  /* Number of used colours */
    LE32_CONST(BMP_NUMCOLORS),  /* Number of important colours */

#if LCD_DEPTH == 1
#ifdef HAVE_NEGATIVE_LCD
    BMP_COLOR(LCD_BL_DARKCOLOR),
    BMP_COLOR(LCD_BL_BRIGHTCOLOR),
#ifdef HAVE_LCD_SPLIT
    BMP_COLOR(LCD_BL_DARKCOLOR_2),
    BMP_COLOR(LCD_BL_BRIGHTCOLOR_2),
#endif
#else /* positive display */
    BMP_COLOR(LCD_BL_BRIGHTCOLOR),
    BMP_COLOR(LCD_BL_DARKCOLOR),
#endif /* positive display */
#elif LCD_DEPTH == 2
    BMP_COLOR(LCD_BL_BRIGHTCOLOR),
    BMP_COLOR_MIX(LCD_BL_BRIGHTCOLOR, LCD_BL_DARKCOLOR, 1, 3),
    BMP_COLOR_MIX(LCD_BL_BRIGHTCOLOR, LCD_BL_DARKCOLOR, 2, 3),
    BMP_COLOR(LCD_BL_DARKCOLOR),
#elif LCD_DEPTH == 16
    0x00, 0xf8, 0x00, 0x00,     /* red bitfield mask */
    0xe0, 0x07, 0x00, 0x00,     /* green bitfield mask */
    0x1f, 0x00, 0x00, 0x00,     /* blue bitfield mask */
#endif
};

static void (*screen_dump_hook)(int fh) = NULL;

void screen_dump(void)
{
    int fd, y;
    char filename[MAX_PATH];

    fb_data *src;
#if LCD_DEPTH == 1
    unsigned mask;
    unsigned val;
#elif (LCD_DEPTH == 2) && (LCD_PIXELFORMAT != HORIZONTAL_PACKING)
    int shift;
    unsigned val;
#endif
#if LCD_DEPTH <= 8
    unsigned char *dst, *dst_end;
    unsigned char linebuf[BMP_LINESIZE];
#elif LCD_DEPTH <= 16
    unsigned short *dst, *dst_end;
    unsigned short linebuf[BMP_LINESIZE/2];
#endif

#if CONFIG_RTC
    create_datetime_filename(filename, "", "dump ", ".bmp", false);
#else
    create_numbered_filename(filename, "", "dump_", ".bmp", 4
                             IF_CNFN_NUM_(, NULL));
#endif

    fd = creat(filename, 0666);
    if (fd < 0)
        return;

    if (screen_dump_hook)
    {
        screen_dump_hook(fd);
    }
    else
    {
        write(fd, bmpheader, sizeof(bmpheader));

        /* BMP image goes bottom up */
        for (y = LCD_HEIGHT - 1; y >= 0; y--)
        {                                  
            memset(linebuf, 0, BMP_LINESIZE);

#if defined(HAVE_LCD_SPLIT) && (LCD_SPLIT_LINES == 2)
            if (y == LCD_SPLIT_POS - 1)
            {
                write(fd, linebuf, BMP_LINESIZE);
                write(fd, linebuf, BMP_LINESIZE);
            }
#endif     
            dst = linebuf;

#if LCD_DEPTH == 1
            dst_end = dst + LCD_WIDTH/2;
            src = lcd_framebuffer[y >> 3];
            mask = BIT_N(y & 7);

            do
            {
                val  = (*src++ & mask) ? 0x10 : 0;
                val |= (*src++ & mask) ? 0x01 : 0;
#ifdef HAVE_LCD_SPLIT
                if (y < LCD_SPLIT_POS)
                    val |= 0x22;
#endif
                *dst++ = val;
            }
            while (dst < dst_end);

#elif LCD_DEPTH == 2
            dst_end = dst + LCD_WIDTH/2;

#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
            src = lcd_framebuffer[y];

            do
            {
                unsigned data = *src++;

                *dst++ = ((data >> 2) & 0x30) | ((data >> 4) & 0x03);
                *dst++ = ((data << 2) & 0x30) | (data & 0x03);
            }
            while (dst < dst_end);

#elif LCD_PIXELFORMAT == VERTICAL_PACKING
            src = lcd_framebuffer[y >> 2];
            shift = 2 * (y & 3);

            do
            {
                val  = ((*src++ >> shift) & 3) << 4;
                val |= ((*src++ >> shift) & 3);
                *dst++ = val;
            }
            while (dst < dst_end);

#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
            src = lcd_framebuffer[y >> 3];
            shift = y & 7;

            do
            {
                unsigned data = (*src++ >> shift) & 0x0101;

                val  = (((data >> 7) | data) & 3) << 4;
                data = (*src++ >> shift) & 0x0101;
                val |= ((data >> 7) | data) & 3;
                *dst++ = val;
            }
            while (dst < dst_end);

#endif
#elif LCD_DEPTH == 16
            dst_end = dst + LCD_WIDTH;
            src = lcd_framebuffer[y];
            
            do
            {
#if (LCD_PIXELFORMAT == RGB565SWAPPED)
                /* iPod LCD data is big endian although the CPU is not */
                *dst++ = htobe16(*src++);
#else
                *dst++ = htole16(*src++);
#endif
            }
            while (dst < dst_end);

#endif /* LCD_DEPTH */
            write(fd, linebuf, BMP_LINESIZE);
        }
    }
    close(fd);
}

void screen_dump_set_hook(void (*hook)(int fh))
{
    screen_dump_hook = hook;
}

#ifdef HAVE_REMOTE_LCD

#define RBMP_COMPRESSION 0 /* BI_RGB */
#define RBMP_NUMCOLORS   (1 << LCD_REMOTE_DEPTH)
#define RBMP_BPP         4
#define RBMP_LINESIZE    ((LCD_REMOTE_WIDTH/2 + 3) & ~3)

#define RBMP_HEADERSIZE (54 + 4 * RBMP_NUMCOLORS)
#define RBMP_DATASIZE   (RBMP_LINESIZE * LCD_REMOTE_HEIGHT)
#define RBMP_TOTALSIZE  (RBMP_HEADERSIZE + RBMP_DATASIZE)

static const unsigned char rbmpheader[] =
{
    0x42, 0x4d,                    /* 'BM' */
    LE32_CONST(RBMP_TOTALSIZE),    /* Total file size */
    0x00, 0x00, 0x00, 0x00,        /* Reserved */
    LE32_CONST(RBMP_HEADERSIZE),   /* Offset to start of pixel data */

    0x28, 0x00, 0x00, 0x00,        /* Size of (2nd) header */
    LE32_CONST(LCD_REMOTE_WIDTH),  /* Width in pixels */
    LE32_CONST(LCD_REMOTE_HEIGHT), /* Height in pixels */
    0x01, 0x00,                    /* Number of planes (always 1) */
    LE16_CONST(RBMP_BPP),          /* Bits per pixel 1/4/8/16/24 */
    LE32_CONST(RBMP_COMPRESSION),  /* Compression mode */
    LE32_CONST(RBMP_DATASIZE),     /* Size of bitmap data */
    0xc4, 0x0e, 0x00, 0x00,        /* Horizontal resolution (pixels/meter) */
    0xc4, 0x0e, 0x00, 0x00,        /* Vertical resolution (pixels/meter) */
    LE32_CONST(RBMP_NUMCOLORS),    /* Number of used colours */
    LE32_CONST(RBMP_NUMCOLORS),    /* Number of important colours */

#if LCD_REMOTE_DEPTH == 1
    BMP_COLOR(LCD_REMOTE_BL_BRIGHTCOLOR),
    BMP_COLOR(LCD_REMOTE_BL_DARKCOLOR),
#elif LCD_REMOTE_DEPTH == 2
    BMP_COLOR(LCD_REMOTE_BL_BRIGHTCOLOR),
    BMP_COLOR_MIX(LCD_REMOTE_BL_BRIGHTCOLOR, LCD_REMOTE_BL_DARKCOLOR, 1, 3),
    BMP_COLOR_MIX(LCD_REMOTE_BL_BRIGHTCOLOR, LCD_REMOTE_BL_DARKCOLOR, 2, 3),
    BMP_COLOR(LCD_REMOTE_BL_DARKCOLOR),
#endif
};

void remote_screen_dump(void)
{
    int fd, y;
    char filename[MAX_PATH];

    fb_remote_data *src;
#if LCD_REMOTE_DEPTH == 1
    unsigned mask;
    unsigned val;
#elif LCD_REMOTE_DEPTH == 2
    int shift;
    unsigned val;
#endif
    unsigned char *dst, *dst_end;
    unsigned char linebuf[RBMP_LINESIZE];

#if CONFIG_RTC
    create_datetime_filename(filename, "", "rdump ", ".bmp", false);
#else
    create_numbered_filename(filename, "", "rdump_", ".bmp", 4
                             IF_CNFN_NUM_(, NULL));
#endif

    fd = creat(filename, 0666);
    if (fd < 0)
        return;

    write(fd, rbmpheader, sizeof(rbmpheader));

    /* BMP image goes bottom up */
    for (y = LCD_REMOTE_HEIGHT - 1; y >= 0; y--)
    {
        memset(linebuf, 0, RBMP_LINESIZE);

        dst = linebuf;

#if LCD_REMOTE_DEPTH == 1
        dst_end = dst + LCD_REMOTE_WIDTH/2;
        src = lcd_remote_framebuffer[y >> 3];
        mask = BIT_N(y & 7);

        do
        {
            val  = (*src++ & mask) ? 0x10 : 0;
            val |= (*src++ & mask) ? 0x01 : 0;
            *dst++ = val;
        }
        while (dst < dst_end);

#elif LCD_REMOTE_DEPTH == 2
        dst_end = dst + LCD_REMOTE_WIDTH/2;

#if LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED
        src = lcd_remote_framebuffer[y >> 3];
        shift = y & 7;

        do
        {
            unsigned data = (*src++ >> shift) & 0x0101;

            val  = (((data >> 7) | data) & 3) << 4;
            data = (*src++ >> shift) & 0x0101;
            val |= ((data >> 7) | data) & 3;
            *dst++ = val;
        }
        while (dst < dst_end);

#endif
#endif /* LCD_REMOTE_DEPTH */
        write(fd, linebuf, RBMP_LINESIZE);
    }
    close(fd);
}

#endif /* HAVE_REMOTE_LCD */