Make creat() posix compliant API-wise. Shouldn't affect the core as it's wrapped...
[kugel-rb.git] / apps / plugins / lib / pluginlib_bmp.c
blob148aa8e1de91ceec5da9bf51754e42b4400eec41
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Antoine Cellerier <dionoea -at- videolan -dot- org>
11 * Copyright (C) 2009 by Andrew Mahone
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "pluginlib_bmp.h"
25 #include "lcd.h"
26 #include "file.h"
27 #include "system.h"
29 #if LCD_DEPTH > 1 /* save is only available for color, resize for >1bpp */
30 #ifdef HAVE_LCD_COLOR
31 #define LE16(x) (htole16(x))&0xff, ((htole16(x))>>8)&0xff
32 #define LE32(x) (htole32(x))&0xff, ((htole32(x))>>8)&0xff, ((htole32(x))>>16)&0xff, ((htole32(x))>>24)&0xff
33 /**
34 * Save to 24 bit bitmap.
36 int save_bmp_file( char* filename, struct bitmap *bm )
38 int line_width = (bm->width*3+3) & ~3;
39 int padsize = line_width - bm->width*3;
41 const unsigned char header[] =
43 0x42, 0x4d, /* signature - 'BM' */
44 LE32( bm->height*line_width + 54 + 4*0 ), /* file size in bytes */
45 0x00, 0x00, 0x00, 0x00, /* 0, 0 */
46 LE32( 54 + 4*0 ), /* offset to bitmap */
47 0x28, 0x00, 0x00, 0x00, /* size of this struct (40) */
48 LE32( bm->width ), /* bmap width in pixels */
49 LE32( bm->height ), /* bmap height in pixels */
50 0x01, 0x00, /* num planes - always 1 */
51 LE16( 24 ), /* bits per pixel */
52 LE32( 0 ), /* compression flag */
53 LE32( bm->height*line_width ), /* image size in bytes */
54 0xc4, 0x0e, 0x00, 0x00, /* horz resolution */
55 0xc4, 0x0e, 0x00, 0x00, /* vert resolution */
56 LE32( 0 ), /* 0 -> color table size */
57 LE32( 0 ) /* important color count */
60 int fh;
61 int x,y;
62 if( bm->format != FORMAT_NATIVE ) return -1;
63 fh = rb->creat( filename , 0666);
64 if( fh < 0 ) return -1;
66 rb->write( fh, header, sizeof( header ) );
67 for( y = bm->height-1; y >= 0; y-- )
69 for( x = 0; x < bm->width; x++ )
71 fb_data *d = (fb_data*)( bm->data ) + (x+y*bm->width);
72 unsigned char c[] =
74 RGB_UNPACK_BLUE( *d ),
75 RGB_UNPACK_GREEN( *d ),
76 RGB_UNPACK_RED( *d )
78 rb->write( fh, c, 3 );
80 if(padsize != 0)
82 unsigned int c = 0; /* padsize is at most 3. */
83 rb->write( fh, &c, padsize );
86 rb->close( fh );
87 return 1;
89 #endif
91 /**
92 Very simple image scale from src to dst (nearest neighbour).
93 Source and destination dimensions are read from the struct bitmap.
95 void simple_resize_bitmap(struct bitmap *src, struct bitmap *dst)
97 const int srcw = src->width;
98 const int srch = src->height;
99 const int dstw = dst->width;
100 const int dsth = dst->height;
101 const fb_data *srcd = (fb_data*)(src->data);
102 const fb_data *dstd = (fb_data*)(dst->data);
104 const long xrstep = ((srcw-1) << 8) / (dstw-1);
105 const long yrstep = ((srch-1) << 8) / (dsth-1);
106 fb_data *src_row, *dst_row;
107 long xr, yr = 0;
108 int src_x, src_y, dst_x, dst_y;
109 for (dst_y=0; dst_y < dsth; dst_y++)
111 src_y = (yr >> 8);
112 src_row = (fb_data*)&srcd[src_y * srcw];
113 dst_row = (fb_data*)&dstd[dst_y * dstw];
114 for (xr=0,dst_x=0; dst_x < dstw; dst_x++)
116 src_x = (xr >> 8);
117 dst_row[dst_x] = src_row[src_x];
118 xr += xrstep;
120 yr += yrstep;
124 #endif /* LCD_DEPTH > 1 */
126 #include "wrappers.h"
128 /* import the core bmp loader */
129 #include "recorder/bmp.c"