root/tags/0.10.0/drv_Image.c

Revision 547, 13.7 kB (checked in by reinelt, 4 years ago)

[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle? added and applied

Line 
1/* $Id: drv_Image.c,v 1.12 2005/05/08 04:32:44 reinelt Exp $
2 *
3 * new style Image (PPM/PNG) Driver for LCD4Linux
4 *
5 * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
6 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
7 *
8 * This file is part of LCD4Linux.
9 *
10 * LCD4Linux is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * LCD4Linux is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *
25 * $Log: drv_Image.c,v $
26 * Revision 1.12  2005/05/08 04:32:44  reinelt
27 * CodingStyle added and applied
28 *
29 * Revision 1.11  2005/01/18 06:30:23  reinelt
30 * added (C) to all copyright statements
31 *
32 * Revision 1.10  2004/11/29 04:42:07  reinelt
33 * removed the 99999 msec limit on widget update time (thanks to Petri Damsten)
34 *
35 * Revision 1.9  2004/06/26 12:04:59  reinelt
36 *
37 * uh-oh... the last CVS log message messed up things a lot...
38 *
39 * Revision 1.8  2004/06/26 09:27:20  reinelt
40 *
41 * added '-W' to CFLAGS
42 * changed all C++ comments to C ones
43 * cleaned up a lot of signed/unsigned mistakes
44 *
45 * Revision 1.7  2004/06/20 10:09:54  reinelt
46 *
47 * 'const'ified the whole source
48 *
49 * Revision 1.6  2004/06/19 08:20:19  reinelt
50 *
51 * compiler warning in image driver fixed
52 * bar bug in USBLCD driver fixed
53 *
54 * Revision 1.5  2004/06/06 06:51:59  reinelt
55 *
56 * do not display end splash screen if quiet=1
57 *
58 * Revision 1.4  2004/06/02 09:41:19  reinelt
59 *
60 * prepared support for startup splash screen
61 *
62 * Revision 1.3  2004/05/31 06:24:42  reinelt
63 *
64 * fixed symlink security issue with the image driver
65 *
66 * Revision 1.2  2004/05/29 23:30:20  reinelt
67 *
68 * fixed a compiler issue with drv_Image.c (thanks to Frank Stratmann)
69 *
70 * Revision 1.1  2004/05/25 14:27:21  reinelt
71 *
72 * added drv_Image.c (this time really!)
73 *
74 */
75
76/*
77 *
78 * exported fuctions:
79 *
80 * struct DRIVER drv_Image
81 *
82 */
83
84
85#include "config.h"
86
87#include <stdlib.h>
88#include <stdio.h>
89#include <string.h>
90#include <errno.h>
91#include <unistd.h>
92#include <termios.h>
93#include <fcntl.h>
94#include <sys/time.h>
95
96#ifdef WITH_PNG
97#ifdef HAVE_GD_GD_H
98#include <gd/gd.h>
99#else
100#ifdef HAVE_GD_H
101#include <gd.h>
102#else
103#error "gd.h not found!"
104#error "cannot compile PNG driver"
105#endif
106#endif
107#endif
108
109
110#include "debug.h"
111#include "cfg.h"
112#include "timer.h"
113#include "qprintf.h"
114#include "plugin.h"
115#include "widget.h"
116#include "widget_text.h"
117#include "widget_icon.h"
118#include "widget_bar.h"
119#include "drv.h"
120#include "drv_generic_graphic.h"
121
122#ifdef WITH_DMALLOC
123#include <dmalloc.h>
124#endif
125
126static char Name[] = "Image";
127
128static enum { PPM, PNG } Format;
129
130static unsigned int fg_col, bg_col, hg_col;
131
132static int pixel = -1;    /* pointsize in pixel */
133static int pgap = 0;    /* gap between points */
134static int rgap = 0;    /* row gap between lines */
135static int cgap = 0;    /* column gap between characters */
136static int border = 0;    /* window border */
137
138static int dimx, dimy;    /* total window dimension in pixel */
139
140static unsigned char *drv_IMG_FB = NULL;
141
142static int dirty = 1;
143
144/****************************************/
145/***  hardware dependant functions    ***/
146/****************************************/
147
148#ifdef WITH_PPM
149static int drv_IMG_flush_PPM(void)
150{
151    static int seq = 0;
152    static unsigned char *bitbuf = NULL;
153    static unsigned char *rowbuf = NULL;
154    int xsize, ysize, row, col;
155    unsigned char R[3], G[3], B[3];
156    char path[256], tmp[256], buffer[256];
157    int fd;
158
159    xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap;
160    ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap;
161
162    if (bitbuf == NULL) {
163  if ((bitbuf = malloc(xsize * ysize * sizeof(*bitbuf))) == NULL) {
164      error("%s: malloc(%d) failed: %s", Name, xsize * ysize * sizeof(*bitbuf), strerror(errno));
165      return -1;
166  }
167    }
168
169    if (rowbuf == NULL) {
170  if ((rowbuf = malloc(3 * xsize * sizeof(*rowbuf))) == NULL) {
171      error("Raster: malloc(%d) failed: %s", 3 * xsize * sizeof(*rowbuf), strerror(errno));
172      return -1;
173  }
174    }
175
176    memset(bitbuf, 0, xsize * ysize * sizeof(*bitbuf));
177
178    for (row = 0; row < DROWS; row++) {
179  int y = border + (row / YRES) * rgap + row * (pixel + pgap);
180  for (col = 0; col < DCOLS; col++) {
181      int x = border + (col / XRES) * cgap + col * (pixel + pgap);
182      int a, b;
183      for (a = 0; a < pixel; a++)
184    for (b = 0; b < pixel; b++)
185        bitbuf[y * xsize + x + a * xsize + b] = drv_IMG_FB[row * DCOLS + col] + 1;
186  }
187    }
188
189    snprintf(path, sizeof(path), output, seq++);
190    qprintf(tmp, sizeof(tmp), "%s.tmp", path);
191
192    /* remove the file */
193    unlink(tmp);
194
195    /* avoid symlink security hole:  */
196    /* open it with O_EXCL will fail if the file exists.  */
197    /* This should not happen because we just unlinked it. */
198    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
199  error("%s: open(%s) failed: %s", Name, tmp, strerror(errno));
200  return -1;
201    }
202
203    qprintf(buffer, sizeof(buffer), "P6\n%d %d\n255\n", xsize, ysize);
204    if (write(fd, buffer, strlen(buffer)) < 0) {
205  error("%s: write(%s) failed: %s", Name, tmp, strerror(errno));
206  return -1;
207    }
208
209    R[0] = 0xff & bg_col >> 16;
210    G[0] = 0xff & bg_col >> 8;
211    B[0] = 0xff & bg_col;
212
213    R[1] = 0xff & hg_col >> 16;
214    G[1] = 0xff & hg_col >> 8;
215    B[1] = 0xff & hg_col;
216
217    R[2] = 0xff & fg_col >> 16;
218    G[2] = 0xff & fg_col >> 8;
219    B[2] = 0xff & fg_col;
220
221    for (row = 0; row < ysize; row++) {
222  int c = 0;
223  for (col = 0; col < xsize; col++) {
224      int i = bitbuf[row * xsize + col];
225      rowbuf[c++] = R[i];
226      rowbuf[c++] = G[i];
227      rowbuf[c++] = B[i];
228  }
229  if (write(fd, rowbuf, c) < 0) {
230      error("%s: write(%s) failed: %s", Name, tmp, strerror(errno));
231      break;
232  }
233    }
234
235    if (close(fd) < 0) {
236  error("%s: close(%s) failed: %s", Name, tmp, strerror(errno));
237  return -1;
238    }
239    if (rename(tmp, path) < 0) {
240  error("%s: rename(%s) failed: %s", Name, tmp, strerror(errno));
241  return -1;
242    }
243
244    return 0;
245}
246#endif
247
248#ifdef WITH_PNG
249static int drv_IMG_flush_PNG(void)
250{
251    static int seq = 0;
252    int xsize, ysize, row, col;
253    char path[256], tmp[256];
254    FILE *fp;
255    int fd;
256    gdImagePtr im;
257    int bg, hg, fg;
258
259    xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap;
260    ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap;
261
262    im = gdImageCreate(xsize, ysize);
263
264    /* first color = background */
265    bg = gdImageColorAllocate(im, 0xff & bg_col >> 16, 0xff & bg_col >> 8, 0xff & bg_col);
266
267    hg = gdImageColorAllocate(im, 0xff & hg_col >> 16, 0xff & hg_col >> 8, 0xff & hg_col);
268
269
270    fg = gdImageColorAllocate(im, 0xff & fg_col >> 16, 0xff & fg_col >> 8, 0xff & fg_col);
271
272
273    for (row = 0; row < DROWS; row++) {
274  int y = border + (row / YRES) * rgap + row * (pixel + pgap);
275  for (col = 0; col < DCOLS; col++) {
276      int x = border + (col / XRES) * cgap + col * (pixel + pgap);
277      gdImageFilledRectangle(im, x, y, x + pixel - 1, y + pixel - 1, drv_IMG_FB[row * DCOLS + col] ? fg : hg);
278  }
279    }
280
281    snprintf(path, sizeof(path), output, seq++);
282    qprintf(tmp, sizeof(tmp), "%s.tmp", path);
283
284    /* remove the file */
285    unlink(tmp);
286
287    /* avoid symlink security hole:  */
288    /* open it with O_EXCL will fail if the file exists.  */
289    /* This should not happen because we just unlinked it. */
290    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
291  error("%s: open(%s) failed: %s", Name, tmp, strerror(errno));
292  return -1;
293    }
294
295    if ((fp = fdopen(fd, "w")) == NULL) {
296  error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno));
297  close(fd);
298  return -1;
299    }
300
301    gdImagePng(im, fp);
302    gdImageDestroy(im);
303
304
305    if (fclose(fp) != 0) {
306  error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno));
307  return -1;
308    }
309
310    if (rename(tmp, path) < 0) {
311  error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno));
312  return -1;
313    }
314
315    return 0;
316}
317#endif
318
319
320static void drv_IMG_flush(void)
321{
322    switch (Format) {
323    case PPM:
324#ifdef WITH_PPM
325  drv_IMG_flush_PPM();
326#endif
327  break;
328    case PNG:
329#ifdef WITH_PNG
330  drv_IMG_flush_PNG();
331#endif
332  break;
333    }
334}
335
336
337static void drv_IMG_timer(void *notused)
338{
339    /* avoid compiler warning */
340    notused = notused;
341
342    if (dirty) {
343  drv_IMG_flush();
344  dirty = 0;
345    }
346}
347
348
349static void drv_IMG_blit(const int row, const int col, const int height, const int width)
350{
351    int r, c;
352
353    for (r = row; r < row + height && r < DROWS; r++) {
354  for (c = col; c < col + width && c < DCOLS; c++) {
355      unsigned char p = drv_generic_graphic_FB[r * LCOLS + c];
356      if (drv_IMG_FB[r * DCOLS + c] != p) {
357    drv_IMG_FB[r * DCOLS + c] = p;
358    dirty = 1;
359      }
360  }
361    }
362}
363
364
365static int drv_IMG_start(const char *section)
366{
367    char *s;
368
369    if (output == NULL || *output == '\0') {
370  error("%s: no output file specified (use -o switch)", Name);
371  return -1;
372    }
373
374    /* read file format from config */
375    s = cfg_get(section, "Format", NULL);
376    if (s == NULL || *s == '\0') {
377  error("%s: no '%s.Format' entry from %s", Name, section, cfg_source());
378  free(s);
379  return -1;
380    }
381
382    if (strcmp(s, "PPM") == 0) {
383  Format = PPM;
384    } else if (strcmp(s, "PNG") == 0) {
385  Format = PNG;
386    } else {
387  error("%s: bad %s.Format '%s' from %s", Name, section, s, cfg_source());
388  free(s);
389  return -1;
390    }
391    free(s);
392
393    /* read display size from config */
394    if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
395  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
396  free(s);
397  return -1;
398    }
399    free(s);
400
401    if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
402  error("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source());
403  free(s);
404  return -1;
405    }
406    free(s);
407
408    if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) {
409  error("%s: bad %s.Pixel '%s' from %s", Name, section, s, cfg_source());
410  free(s);
411  return -1;
412    }
413    free(s);
414
415    if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) {
416  error("%s: bad %s.Gap '%s' from %s", Name, section, s, cfg_source());
417  free(s);
418  return -1;
419    }
420    free(s);
421
422    if (rgap < 0)
423  rgap = pixel + pgap;
424    if (cgap < 0)
425  cgap = pixel + pgap;
426
427    if (cfg_number(section, "border", 0, 0, -1, &border) < 0)
428  return -1;
429
430    if (sscanf(s = cfg_get(NULL, "foreground", "#102000"), "#%x", &fg_col) != 1) {
431  error("%s: bad %s.foreground color '%s' from %s", Name, section, s, cfg_source());
432  free(s);
433  return -1;
434    }
435    free(s);
436
437    if (sscanf(s = cfg_get(NULL, "halfground", "#70c000"), "#%x", &hg_col) != 1) {
438  error("%s: bad %s.halfground color '%s' from %s", Name, section, s, cfg_source());
439  free(s);
440  return -1;
441    }
442    free(s);
443
444    if (sscanf(s = cfg_get(NULL, "background", "#80d000"), "#%x", &bg_col) != 1) {
445  error("%s: bad %s.background color '%s' from %s", Name, section, s, cfg_source());
446  free(s);
447  return -1;
448    }
449    free(s);
450
451    drv_IMG_FB = malloc(DCOLS * DROWS);
452    if (drv_IMG_FB == NULL) {
453  error("%s: framebuffer could not be allocated: malloc() failed", Name);
454  return -1;
455    }
456
457    memset(drv_IMG_FB, 0, DCOLS * DROWS * sizeof(*drv_IMG_FB));
458
459
460    dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap;
461    dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap;
462
463
464    /* initially flush the image to a file */
465    drv_IMG_flush();
466
467    /* regularly flush the image to a file */
468    /* Fixme: make 100msec configurable */
469    timer_add(drv_IMG_timer, NULL, 100, 0);
470
471
472    return 0;
473}
474
475
476
477/****************************************/
478/***            plugins               ***/
479/****************************************/
480
481/* none at the moment... */
482
483
484/****************************************/
485/***        widget callbacks          ***/
486/****************************************/
487
488
489/* using drv_generic_graphic_draw(W) */
490/* using drv_generic_graphic_icon_draw(W) */
491/* using drv_generic_graphic_bar_draw(W) */
492
493
494/****************************************/
495/***        exported functions        ***/
496/****************************************/
497
498
499/* list models */
500int drv_IMG_list(void)
501{
502    printf("PPM PNG");
503    return 0;
504}
505
506
507/* initialize driver & display */
508int drv_IMG_init(const char *section, const __attribute__ ((unused))
509     int quiet)
510{
511    WIDGET_CLASS wc;
512    int ret;
513
514    /* real worker functions */
515    drv_generic_graphic_real_blit = drv_IMG_blit;
516
517    /* start display */
518    if ((ret = drv_IMG_start(section)) != 0)
519  return ret;
520
521    /* initialize generic graphic driver */
522    if ((ret = drv_generic_graphic_init(section, Name)) != 0)
523  return ret;
524
525    /* register text widget */
526    wc = Widget_Text;
527    wc.draw = drv_generic_graphic_draw;
528    widget_register(&wc);
529
530    /* register icon widget */
531    wc = Widget_Icon;
532    wc.draw = drv_generic_graphic_icon_draw;
533    widget_register(&wc);
534
535    /* register bar widget */
536    wc = Widget_Bar;
537    wc.draw = drv_generic_graphic_bar_draw;
538    widget_register(&wc);
539
540    /* register plugins */
541    /* none at the moment... */
542
543
544    return 0;
545}
546
547
548/* close driver & display */
549int drv_IMG_quit(const __attribute__ ((unused))
550     int quiet)
551{
552
553    info("%s: shutting down.", Name);
554    drv_generic_graphic_quit();
555
556    if (drv_IMG_FB) {
557  free(drv_IMG_FB);
558  drv_IMG_FB = NULL;
559    }
560
561    return (0);
562}
563
564
565DRIVER drv_Image = {
566  name:Name,
567  list:drv_IMG_list,
568  init:drv_IMG_init,
569  quit:drv_IMG_quit,
570};
Note: See TracBrowser for help on using the browser.