summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrank Gevaerts <frank@gevaerts.be>2008-06-30 18:17:58 +0000
committerFrank Gevaerts <frank@gevaerts.be>2008-06-30 18:17:58 +0000
commit97a143279b78799d722e3f992e1a59e32ef3c489 (patch)
tree1d06cd4dd0bfcfc0c6d3f1ff01038d5d4703b509
parentf69982bb0b24769ad3dd5dc543f50ce444bc182c (diff)
downloadrockbox-97a143279b78799d722e3f992e1a59e32ef3c489.tar.gz
rockbox-97a143279b78799d722e3f992e1a59e32ef3c489.zip
fix line endings and properties
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17892 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/ppmviewer.c664
1 files changed, 332 insertions, 332 deletions
diff --git a/apps/plugins/ppmviewer.c b/apps/plugins/ppmviewer.c
index 04d4b001a6..4c24a5d5f4 100644
--- a/apps/plugins/ppmviewer.c
+++ b/apps/plugins/ppmviewer.c
@@ -1,336 +1,336 @@
1/***************************************************************************** 1/*****************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// __ \_/ ___\| |/ /| __ \ / __ \ \/ / 4 * Source | _// __ \_/ ___\| |/ /| __ \ / __ \ \/ /
5 * Jukebox | | ( (__) ) \___| ( | \_\ ( (__) ) ( 5 * Jukebox | | ( (__) ) \___| ( | \_\ ( (__) ) (
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 Alexander Papst 10 * Copyright (C) 2008 Alexander Papst
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include "plugin.h" 22#include "plugin.h"
23#include "bmp.h" 23#include "bmp.h"
24 24
25#if defined(HAVE_LCD_COLOR) 25#if defined(HAVE_LCD_COLOR)
26 26
27PLUGIN_HEADER 27PLUGIN_HEADER
28 28
29/* Magic constants. */ 29/* Magic constants. */
30#define PPM_MAGIC1 'P' 30#define PPM_MAGIC1 'P'
31#define PPM_MAGIC2 '3' 31#define PPM_MAGIC2 '3'
32#define RPPM_MAGIC2 '6' 32#define RPPM_MAGIC2 '6'
33#define PPM_FORMAT (PPM_MAGIC1 * 256 + PPM_MAGIC2) 33#define PPM_FORMAT (PPM_MAGIC1 * 256 + PPM_MAGIC2)
34#define RPPM_FORMAT (PPM_MAGIC1 * 256 + RPPM_MAGIC2) 34#define RPPM_FORMAT (PPM_MAGIC1 * 256 + RPPM_MAGIC2)
35 35
36#define PPM_OVERALLMAXVAL 65535 36#define PPM_OVERALLMAXVAL 65535
37#define PPM_MAXSIZE (300*1024)/sizeof(fb_data) 37#define PPM_MAXSIZE (300*1024)/sizeof(fb_data)
38 38
39#define ppm_error(...) rb->splash(HZ*2, __VA_ARGS__ ) 39#define ppm_error(...) rb->splash(HZ*2, __VA_ARGS__ )
40 40
41static fb_data buffer[PPM_MAXSIZE]; 41static fb_data buffer[PPM_MAXSIZE];
42static fb_data lcd_buf[LCD_WIDTH * LCD_HEIGHT]; 42static fb_data lcd_buf[LCD_WIDTH * LCD_HEIGHT];
43 43
44static const struct plugin_api* rb; /* global api struct pointer */ 44static const struct plugin_api* rb; /* global api struct pointer */
45 45
46int ppm_read_magic_number(int fd) 46int ppm_read_magic_number(int fd)
47{ 47{
48 char i1, i2; 48 char i1, i2;
49 if(!rb->read(fd, &i1, 1) || !rb->read(fd, &i2, 1)) 49 if(!rb->read(fd, &i1, 1) || !rb->read(fd, &i2, 1))
50 { 50 {
51 ppm_error( "Error reading magic number from ppm image stream. "\ 51 ppm_error( "Error reading magic number from ppm image stream. "\
52 "Most often, this means your input file is empty." ); 52 "Most often, this means your input file is empty." );
53 return PLUGIN_ERROR; 53 return PLUGIN_ERROR;
54 } 54 }
55 return i1 * 256 + i2; 55 return i1 * 256 + i2;
56} 56}
57 57
58char ppm_getc(int fd) 58char ppm_getc(int fd)
59{ 59{
60 char ch; 60 char ch;
61 61
62 if (!rb->read(fd, &ch, 1)) { 62 if (!rb->read(fd, &ch, 1)) {
63 ppm_error("EOF. Read error reading a byte"); 63 ppm_error("EOF. Read error reading a byte");
64 return PLUGIN_ERROR; 64 return PLUGIN_ERROR;
65 } 65 }
66 66
67 if (ch == '#') { 67 if (ch == '#') {
68 do { 68 do {
69 if (!rb->read(fd, &ch, 1)) { 69 if (!rb->read(fd, &ch, 1)) {
70 ppm_error("EOF. Read error reading a byte"); 70 ppm_error("EOF. Read error reading a byte");
71 return PLUGIN_ERROR; 71 return PLUGIN_ERROR;
72 } 72 }
73 } while (ch != '\n' && ch != '\r'); 73 } while (ch != '\n' && ch != '\r');
74 } 74 }
75 return ch; 75 return ch;
76} 76}
77 77
78int ppm_getuint(int fd) 78int ppm_getuint(int fd)
79{ 79{
80 char ch; 80 char ch;
81 int i; 81 int i;
82 int digitVal; 82 int digitVal;
83 83
84 do { 84 do {
85 ch = ppm_getc(fd); 85 ch = ppm_getc(fd);
86 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); 86 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
87 87
88 if (ch < '0' || ch > '9') { 88 if (ch < '0' || ch > '9') {
89 ppm_error("Junk (%c) in file where an integer should be.", ch); 89 ppm_error("Junk (%c) in file where an integer should be.", ch);
90 return PLUGIN_ERROR; 90 return PLUGIN_ERROR;
91 } 91 }
92 92
93 i = 0; 93 i = 0;
94 94
95 do { 95 do {
96 digitVal = ch - '0'; 96 digitVal = ch - '0';
97 97
98 if (i > INT_MAX/10 - digitVal) { 98 if (i > INT_MAX/10 - digitVal) {
99 ppm_error("ASCII decimal integer in file is "\ 99 ppm_error("ASCII decimal integer in file is "\
100 "too large to be processed."); 100 "too large to be processed.");
101 return PLUGIN_ERROR; 101 return PLUGIN_ERROR;
102 } 102 }
103 103
104 i = i * 10 + digitVal; 104 i = i * 10 + digitVal;
105 ch = ppm_getc(fd); 105 ch = ppm_getc(fd);
106 106
107 } while (ch >= '0' && ch <= '9'); 107 } while (ch >= '0' && ch <= '9');
108 108
109 return i; 109 return i;
110} 110}
111 111
112int ppm_getrawbyte(int fd) 112int ppm_getrawbyte(int fd)
113{ 113{
114 unsigned char by; 114 unsigned char by;
115 115
116 if (!rb->read(fd, &by, 1)) { 116 if (!rb->read(fd, &by, 1)) {
117 ppm_error("EOF. Read error while reading a one-byte sample."); 117 ppm_error("EOF. Read error while reading a one-byte sample.");
118 return PLUGIN_ERROR; 118 return PLUGIN_ERROR;
119 } 119 }
120 120
121 return (int)by; 121 return (int)by;
122} 122}
123 123
124int ppm_getrawsample(int fd, int const maxval) 124int ppm_getrawsample(int fd, int const maxval)
125{ 125{
126 if (maxval < 256) { 126 if (maxval < 256) {
127 /* The sample is just one byte. Read it. */ 127 /* The sample is just one byte. Read it. */
128 return(ppm_getrawbyte(fd)); 128 return(ppm_getrawbyte(fd));
129 } else { 129 } else {
130 /* The sample is two bytes. Read both. */ 130 /* The sample is two bytes. Read both. */
131 unsigned char byte_pair[2]; 131 unsigned char byte_pair[2];
132 132
133 if (!rb->read(fd, byte_pair, 2)) { 133 if (!rb->read(fd, byte_pair, 2)) {
134 ppm_error("EOF. Read error while reading a long sample."); 134 ppm_error("EOF. Read error while reading a long sample.");
135 return PLUGIN_ERROR; 135 return PLUGIN_ERROR;
136 } 136 }
137 return((byte_pair[0]<<8) | byte_pair[1]); 137 return((byte_pair[0]<<8) | byte_pair[1]);
138 } 138 }
139} 139}
140 140
141int read_ppm_init_rest(int fd, 141int read_ppm_init_rest(int fd,
142 int * const cols, 142 int * const cols,
143 int * const rows, 143 int * const rows,
144 int * const maxval) 144 int * const maxval)
145{ 145{
146 /* Read size. */ 146 /* Read size. */
147 *cols = ppm_getuint(fd); 147 *cols = ppm_getuint(fd);
148 *rows = ppm_getuint(fd); 148 *rows = ppm_getuint(fd);
149 149
150 if ((long unsigned int)(*cols * *rows) > PPM_MAXSIZE) { 150 if ((long unsigned int)(*cols * *rows) > PPM_MAXSIZE) {
151 ppm_error("Imagesize (%ld pixels) is too large. "\ 151 ppm_error("Imagesize (%ld pixels) is too large. "\
152 "The maximum allowed is %ld.", 152 "The maximum allowed is %ld.",
153 (long unsigned int)(*cols * *rows), 153 (long unsigned int)(*cols * *rows),
154 (long unsigned int)PPM_MAXSIZE); 154 (long unsigned int)PPM_MAXSIZE);
155 return PLUGIN_ERROR; 155 return PLUGIN_ERROR;
156 } 156 }
157 157
158 /* Read maxval. */ 158 /* Read maxval. */
159 *maxval = ppm_getuint(fd); 159 *maxval = ppm_getuint(fd);
160 160
161 if (*maxval > PPM_OVERALLMAXVAL) { 161 if (*maxval > PPM_OVERALLMAXVAL) {
162 ppm_error("maxval of input image (%u) is too large. "\ 162 ppm_error("maxval of input image (%u) is too large. "\
163 "The maximum allowed by the PPM is %u.", 163 "The maximum allowed by the PPM is %u.",
164 *maxval, PPM_OVERALLMAXVAL); 164 *maxval, PPM_OVERALLMAXVAL);
165 return PLUGIN_ERROR; 165 return PLUGIN_ERROR;
166 } 166 }
167 if (*maxval == 0) { 167 if (*maxval == 0) {
168 ppm_error("maxval of input image is zero."); 168 ppm_error("maxval of input image is zero.");
169 return PLUGIN_ERROR; 169 return PLUGIN_ERROR;
170 } 170 }
171 return 1; 171 return 1;
172} 172}
173 173
174void read_ppm_init(int fd, 174void read_ppm_init(int fd,
175 int * const cols, 175 int * const cols,
176 int * const rows, 176 int * const rows,
177 int * const maxval, 177 int * const maxval,
178 int * const format) 178 int * const format)
179{ 179{
180 /* Check magic number. */ 180 /* Check magic number. */
181 *format = ppm_read_magic_number( fd ); 181 *format = ppm_read_magic_number( fd );
182 182
183 if (*format == PLUGIN_ERROR) return; 183 if (*format == PLUGIN_ERROR) return;
184 switch (*format) { 184 switch (*format) {
185 case PPM_FORMAT: 185 case PPM_FORMAT:
186 case RPPM_FORMAT: 186 case RPPM_FORMAT:
187 if(read_ppm_init_rest(fd, cols, rows, maxval) == PLUGIN_ERROR) { 187 if(read_ppm_init_rest(fd, cols, rows, maxval) == PLUGIN_ERROR) {
188 *format = PLUGIN_ERROR; 188 *format = PLUGIN_ERROR;
189 } 189 }
190 break; 190 break;
191 191
192 default: 192 default:
193 ppm_error( "Bad magic number - not a ppm or rppm file." ); 193 ppm_error( "Bad magic number - not a ppm or rppm file." );
194 *format = PLUGIN_ERROR; 194 *format = PLUGIN_ERROR;
195 } 195 }
196} 196}
197 197
198int read_ppm_row(int fd, 198int read_ppm_row(int fd,
199 int const row, 199 int const row,
200 int const cols, 200 int const cols,
201 int const maxval, 201 int const maxval,
202 int const format) 202 int const format)
203{ 203{
204 int col; 204 int col;
205 int r, g, b; 205 int r, g, b;
206 switch (format) { 206 switch (format) {
207 case PPM_FORMAT: 207 case PPM_FORMAT:
208 for (col = 0; col < cols; ++col) { 208 for (col = 0; col < cols; ++col) {
209 r = ppm_getuint(fd); 209 r = ppm_getuint(fd);
210 g = ppm_getuint(fd); 210 g = ppm_getuint(fd);
211 b = ppm_getuint(fd); 211 b = ppm_getuint(fd);
212 212
213 if (r == PLUGIN_ERROR || g == PLUGIN_ERROR || 213 if (r == PLUGIN_ERROR || g == PLUGIN_ERROR ||
214 b == PLUGIN_ERROR) 214 b == PLUGIN_ERROR)
215 { 215 {
216 return PLUGIN_ERROR; 216 return PLUGIN_ERROR;
217 } 217 }
218 buffer[(cols * row) + col] = LCD_RGBPACK( 218 buffer[(cols * row) + col] = LCD_RGBPACK(
219 (255 / maxval) * r, 219 (255 / maxval) * r,
220 (255 / maxval) * g, 220 (255 / maxval) * g,
221 (255 / maxval) * b); 221 (255 / maxval) * b);
222 } 222 }
223 break; 223 break;
224 224
225 case RPPM_FORMAT: 225 case RPPM_FORMAT:
226 for (col = 0; col < cols; ++col) { 226 for (col = 0; col < cols; ++col) {
227 r = ppm_getrawsample(fd, maxval); 227 r = ppm_getrawsample(fd, maxval);
228 g = ppm_getrawsample(fd, maxval); 228 g = ppm_getrawsample(fd, maxval);
229 b = ppm_getrawsample(fd, maxval); 229 b = ppm_getrawsample(fd, maxval);
230 230
231 if (r == PLUGIN_ERROR || g == PLUGIN_ERROR || 231 if (r == PLUGIN_ERROR || g == PLUGIN_ERROR ||
232 b == PLUGIN_ERROR) 232 b == PLUGIN_ERROR)
233 { 233 {
234 return PLUGIN_ERROR; 234 return PLUGIN_ERROR;
235 } 235 }
236 buffer[(cols * row) + col] = LCD_RGBPACK( 236 buffer[(cols * row) + col] = LCD_RGBPACK(
237 (255 / maxval) * r, 237 (255 / maxval) * r,
238 (255 / maxval) * g, 238 (255 / maxval) * g,
239 (255 / maxval) * b); 239 (255 / maxval) * b);
240 } 240 }
241 break; 241 break;
242 242
243 default: 243 default:
244 ppm_error("What?!"); 244 ppm_error("What?!");
245 return PLUGIN_ERROR; 245 return PLUGIN_ERROR;
246 } 246 }
247 return 1; 247 return 1;
248} 248}
249 249
250int read_ppm(int fd, 250int read_ppm(int fd,
251 int * const cols, 251 int * const cols,
252 int * const rows, 252 int * const rows,
253 int * const maxval) 253 int * const maxval)
254{ 254{
255 int row; 255 int row;
256 int format; 256 int format;
257 257
258 read_ppm_init(fd, cols, rows, maxval, &format); 258 read_ppm_init(fd, cols, rows, maxval, &format);
259 259
260 if(format == PLUGIN_ERROR) { 260 if(format == PLUGIN_ERROR) {
261 return PLUGIN_ERROR; 261 return PLUGIN_ERROR;
262 } 262 }
263 263
264 for (row = 0; row < *rows; ++row) { 264 for (row = 0; row < *rows; ++row) {
265 if( read_ppm_row(fd, row, *cols, *maxval, format) == PLUGIN_ERROR) { 265 if( read_ppm_row(fd, row, *cols, *maxval, format) == PLUGIN_ERROR) {
266 return PLUGIN_ERROR; 266 return PLUGIN_ERROR;
267 } 267 }
268 } 268 }
269 return 1; 269 return 1;
270} 270}
271 271
272/* this is the plugin entry point */ 272/* this is the plugin entry point */
273enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) 273enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
274{ 274{
275 static char filename[MAX_PATH]; 275 static char filename[MAX_PATH];
276 int fd; 276 int fd;
277 277
278 int cols; 278 int cols;
279 int rows; 279 int rows;
280 int maxval; 280 int maxval;
281 281
282 int result; 282 int result;
283 283
284 struct bitmap small_bitmap, orig_bitmap; 284 struct bitmap small_bitmap, orig_bitmap;
285 285
286 if(!parameter) return PLUGIN_ERROR; 286 if(!parameter) return PLUGIN_ERROR;
287 287
288 rb = api; 288 rb = api;
289 289
290 rb->strcpy(filename, parameter); 290 rb->strcpy(filename, parameter);
291 291
292 fd = rb->open(filename, O_RDONLY); 292 fd = rb->open(filename, O_RDONLY);
293 if (fd < 0) 293 if (fd < 0)
294 { 294 {
295 ppm_error("Couldnt open file: %s, %d", filename, fd); 295 ppm_error("Couldnt open file: %s, %d", filename, fd);
296 return PLUGIN_ERROR; 296 return PLUGIN_ERROR;
297 } 297 }
298 298
299 result = read_ppm(fd, &cols, &rows, &maxval); 299 result = read_ppm(fd, &cols, &rows, &maxval);
300 300
301 rb->close(fd); 301 rb->close(fd);
302 if(result == PLUGIN_ERROR) return PLUGIN_ERROR; 302 if(result == PLUGIN_ERROR) return PLUGIN_ERROR;
303 303
304 orig_bitmap.width = cols; 304 orig_bitmap.width = cols;
305 orig_bitmap.height = rows; 305 orig_bitmap.height = rows;
306 orig_bitmap.data = (char*)buffer; 306 orig_bitmap.data = (char*)buffer;
307 307
308 if (cols > LCD_WIDTH || rows > LCD_HEIGHT) 308 if (cols > LCD_WIDTH || rows > LCD_HEIGHT)
309 { 309 {
310 if (cols > LCD_WIDTH) { 310 if (cols > LCD_WIDTH) {
311 small_bitmap.width = LCD_WIDTH; 311 small_bitmap.width = LCD_WIDTH;
312 small_bitmap.height = 312 small_bitmap.height =
313 (int)(((float)LCD_WIDTH / (float)cols) * (float)rows); 313 (int)(((float)LCD_WIDTH / (float)cols) * (float)rows);
314 314
315 } else { /* rows > LCD_HEIGHT */ 315 } else { /* rows > LCD_HEIGHT */
316 316
317 small_bitmap.width = 317 small_bitmap.width =
318 (int)(((float)LCD_HEIGHT / (float)rows) * (float)cols); 318 (int)(((float)LCD_HEIGHT / (float)rows) * (float)cols);
319 small_bitmap.height = LCD_HEIGHT; 319 small_bitmap.height = LCD_HEIGHT;
320 } 320 }
321 small_bitmap.data = (char*)lcd_buf; 321 small_bitmap.data = (char*)lcd_buf;
322 322
323 smooth_resize_bitmap( &orig_bitmap, &small_bitmap ); 323 smooth_resize_bitmap( &orig_bitmap, &small_bitmap );
324 324
325 rb->lcd_bitmap((fb_data*)small_bitmap.data, 0, 0, 325 rb->lcd_bitmap((fb_data*)small_bitmap.data, 0, 0,
326 small_bitmap.width, small_bitmap.height); 326 small_bitmap.width, small_bitmap.height);
327 } else { 327 } else {
328 rb->lcd_bitmap((fb_data*)orig_bitmap.data, 0, 0, cols, rows); 328 rb->lcd_bitmap((fb_data*)orig_bitmap.data, 0, 0, cols, rows);
329 } 329 }
330 rb->lcd_update(); 330 rb->lcd_update();
331 rb->button_get(true); 331 rb->button_get(true);
332 332
333 return PLUGIN_OK; 333 return PLUGIN_OK;
334} 334}
335 335
336#endif 336#endif