root/branches/0.10.1/drv_Image.c

Revision 773, 10.8 kB (checked in by michael, 23 months ago)

backport 768:771 from trunk, version changed to 0.10.1-RC2

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style Image (PPM/PNG) Driver for LCD4Linux
5 *
6 * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * This file is part of LCD4Linux.
10 *
11 * LCD4Linux is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * LCD4Linux is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27/*
28 *
29 * exported fuctions:
30 *
31 * struct DRIVER drv_Image
32 *
33 */
34
35
36#include "config.h"
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42#include <unistd.h>
43#include <termios.h>
44#include <fcntl.h>
45#include <sys/time.h>
46
47
48#ifdef WITH_PNG
49
50#ifdef HAVE_GD_GD_H
51#include <gd/gd.h>
52#else
53#ifdef HAVE_GD_H
54#include <gd.h>
55#else
56#error "gd.h not found!"
57#error "cannot compile PNG driver"
58#endif
59#endif
60
61#if GD2_VERS != 2
62#error "lcd4linux requires libgd version 2"
63#error "cannot compile PNG driver"
64#endif
65
66#endif
67
68
69#include "debug.h"
70#include "cfg.h"
71#include "timer.h"
72#include "qprintf.h"
73#include "plugin.h"
74#include "drv.h"
75#include "drv_generic_graphic.h"
76
77#ifdef WITH_DMALLOC
78#include <dmalloc.h>
79#endif
80
81static char Name[] = "Image";
82
83static enum { PPM, PNG } Format;
84
85static int pixel = -1;    /* pointsize in pixel */
86static int pgap = 0;    /* gap between points */
87static int rgap = 0;    /* row gap between lines */
88static int cgap = 0;    /* column gap between characters */
89static int border = 0;    /* window border */
90
91static int dimx, dimy;    /* total window dimension in pixel */
92
93static RGBA BC;
94static RGBA *drv_IMG_FB = NULL;
95
96static int dirty = 1;
97
98/****************************************/
99/***  hardware dependant functions    ***/
100/****************************************/
101
102#ifdef WITH_PPM
103static int drv_IMG_flush_PPM(void)
104{
105    static int seq = 0;
106    static RGBA *bitbuf = NULL;
107    static unsigned char *rowbuf = NULL;
108    int xsize, ysize, row, col, i;
109    char path[256], tmp[256], buffer[256];
110    int fd;
111
112    xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap;
113    ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap;
114
115    if (bitbuf == NULL) {
116  if ((bitbuf = malloc(xsize * ysize * sizeof(*bitbuf))) == NULL) {
117      error("%s: malloc(%d) failed: %s", Name, (int) xsize * ysize * sizeof(*bitbuf), strerror(errno));
118      return -1;
119  }
120    }
121
122    if (rowbuf == NULL) {
123  if ((rowbuf = malloc(3 * xsize * sizeof(*rowbuf))) == NULL) {
124      error("Raster: malloc(%d) failed: %s", (int) 3 * xsize * sizeof(*rowbuf), strerror(errno));
125      return -1;
126  }
127    }
128
129    for (i = 0; i < xsize * ysize; i++) {
130  bitbuf[i] = BC;
131    }
132
133    for (row = 0; row < DROWS; row++) {
134  int y = border + (row / YRES) * rgap + row * (pixel + pgap);
135  for (col = 0; col < DCOLS; col++) {
136      int x = border + (col / XRES) * cgap + col * (pixel + pgap);
137      int a, b;
138      for (a = 0; a < pixel; a++)
139    for (b = 0; b < pixel; b++)
140        bitbuf[y * xsize + x + a * xsize + b] = drv_IMG_FB[row * DCOLS + col];
141  }
142    }
143
144    snprintf(path, sizeof(path), output, seq++);
145    qprintf(tmp, sizeof(tmp), "%s.tmp", path);
146
147    /* remove the file */
148    unlink(tmp);
149
150    /* avoid symlink security hole:  */
151    /* open it with O_EXCL will fail if the file exists.  */
152    /* This should not happen because we just unlinked it. */
153    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
154  error("%s: open(%s) failed: %s", Name, tmp, strerror(errno));
155  return -1;
156    }
157
158    qprintf(buffer, sizeof(buffer), "P6\n%d %d\n255\n", xsize, ysize);
159    if (write(fd, buffer, strlen(buffer)) < 0) {
160  error("%s: write(%s) failed: %s", Name, tmp, strerror(errno));
161  return -1;
162    }
163
164    for (row = 0; row < ysize; row++) {
165  int c = 0;
166  for (col = 0; col < xsize; col++) {
167      RGBA p = bitbuf[row * xsize + col];
168      rowbuf[c++] = p.R;
169      rowbuf[c++] = p.G;
170      rowbuf[c++] = p.B;
171  }
172  if (write(fd, rowbuf, c) < 0) {
173      error("%s: write(%s) failed: %s", Name, tmp, strerror(errno));
174      break;
175  }
176    }
177
178    if (close(fd) < 0) {
179  error("%s: close(%s) failed: %s", Name, tmp, strerror(errno));
180  return -1;
181    }
182    if (rename(tmp, path) < 0) {
183  error("%s: rename(%s) failed: %s", Name, tmp, strerror(errno));
184  return -1;
185    }
186
187    return 0;
188}
189#endif
190
191#ifdef WITH_PNG
192static int drv_IMG_flush_PNG(void)
193{
194    static int seq = 0;
195    int xsize, ysize, row, col;
196    char path[256], tmp[256];
197    FILE *fp;
198    int fd;
199    gdImagePtr im;
200
201    xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap;
202    ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap;
203
204    im = gdImageCreateTrueColor(xsize, ysize);
205    gdImageFilledRectangle(im, 0, 0, xsize, ysize, gdTrueColor(BC.R, BC.G, BC.B));
206
207    for (row = 0; row < DROWS; row++) {
208  int y = border + (row / YRES) * rgap + row * (pixel + pgap);
209  for (col = 0; col < DCOLS; col++) {
210      int x = border + (col / XRES) * cgap + col * (pixel + pgap);
211      RGBA p = drv_IMG_FB[row * DCOLS + col];
212      int c = gdTrueColor(p.R, p.G, p.B);
213      gdImageFilledRectangle(im, x, y, x + pixel - 1, y + pixel - 1, c);
214  }
215    }
216
217    snprintf(path, sizeof(path), output, seq++);
218    qprintf(tmp, sizeof(tmp), "%s.tmp", path);
219
220    /* remove the file */
221    unlink(tmp);
222
223    /* avoid symlink security hole:  */
224    /* open it with O_EXCL will fail if the file exists.  */
225    /* This should not happen because we just unlinked it. */
226    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
227  error("%s: open(%s) failed: %s", Name, tmp, strerror(errno));
228  return -1;
229    }
230
231    if ((fp = fdopen(fd, "w")) == NULL) {
232  error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno));
233  close(fd);
234  return -1;
235    }
236
237    gdImagePng(im, fp);
238    gdImageDestroy(im);
239
240
241    if (fclose(fp) != 0) {
242  error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno));
243  return -1;
244    }
245
246    if (rename(tmp, path) < 0) {
247  error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno));
248  return -1;
249    }
250
251    return 0;
252}
253#endif
254
255
256static void drv_IMG_flush(void)
257{
258    switch (Format) {
259    case PPM:
260#ifdef WITH_PPM
261  drv_IMG_flush_PPM();
262#endif
263  break;
264    case PNG:
265#ifdef WITH_PNG
266  drv_IMG_flush_PNG();
267#endif
268  break;
269    }
270}
271
272
273static void drv_IMG_timer( __attribute__ ((unused))
274        void *notused)
275{
276    if (dirty) {
277  drv_IMG_flush();
278  dirty = 0;
279    }
280}
281
282
283static void drv_IMG_blit(const int row, const int col, const int height, const int width)
284{
285    int r, c;
286
287    for (r = row; r < row + height; r++) {
288  for (c = col; c < col + width; c++) {
289      RGBA p1 = drv_IMG_FB[r * DCOLS + c];
290      RGBA p2 = drv_generic_graphic_rgb(r, c);
291      if (p1.R != p2.R || p1.G != p2.G || p1.B != p2.B) {
292    drv_IMG_FB[r * DCOLS + c] = p2;
293    dirty = 1;
294      }
295  }
296    }
297}
298
299
300static int drv_IMG_start(const char *section)
301{
302    int i;
303    char *s;
304
305    if (output == NULL || *output == '\0') {
306  error("%s: no output file specified (use -o switch)", Name);
307  return -1;
308    }
309
310    /* read file format from config */
311    s = cfg_get(section, "Format", NULL);
312    if (s == NULL || *s == '\0') {
313  error("%s: no '%s.Format' entry from %s", Name, section, cfg_source());
314  free(s);
315  return -1;
316    }
317
318    if (strcmp(s, "PPM") == 0) {
319  Format = PPM;
320    } else if (strcmp(s, "PNG") == 0) {
321  Format = PNG;
322    } else {
323  error("%s: bad %s.Format '%s' from %s", Name, section, s, cfg_source());
324  free(s);
325  return -1;
326    }
327    free(s);
328
329    /* read display size from config */
330    if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
331  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
332  free(s);
333  return -1;
334    }
335    free(s);
336
337    if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
338  error("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source());
339  free(s);
340  return -1;
341    }
342    free(s);
343
344    if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) {
345  error("%s: bad %s.Pixel '%s' from %s", Name, section, s, cfg_source());
346  free(s);
347  return -1;
348    }
349    free(s);
350
351    if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) {
352  error("%s: bad %s.Gap '%s' from %s", Name, section, s, cfg_source());
353  free(s);
354  return -1;
355    }
356    free(s);
357
358    if (rgap < 0)
359  rgap = pixel + pgap;
360    if (cgap < 0)
361  cgap = pixel + pgap;
362
363    if (cfg_number(section, "border", 0, 0, -1, &border) < 0)
364  return -1;
365
366    s = cfg_get(section, "basecolor", "000000ff");
367    if (color2RGBA(s, &BC) < 0) {
368  error("%s: ignoring illegal color '%s'", Name, s);
369    }
370    free(s);
371
372    drv_IMG_FB = malloc(DCOLS * DROWS * sizeof(*drv_IMG_FB));
373    if (drv_IMG_FB == NULL) {
374  error("%s: framebuffer could not be allocated: malloc() failed", Name);
375  return -1;
376    }
377
378    for (i = 0; i < DCOLS * DROWS; i++) {
379  drv_IMG_FB[i] = BC;
380    }
381
382    dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap;
383    dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap;
384
385
386    /* initially flush the image to a file */
387    drv_IMG_flush();
388
389    /* regularly flush the image to a file */
390    /* Fixme: make 100msec configurable */
391    timer_add(drv_IMG_timer, NULL, 100, 0);
392
393
394    return 0;
395}
396
397
398
399/****************************************/
400/***            plugins               ***/
401/****************************************/
402
403/* none at the moment... */
404
405
406/****************************************/
407/***        exported functions        ***/
408/****************************************/
409
410
411/* list models */
412int drv_IMG_list(void)
413{
414    printf("PPM PNG");
415    return 0;
416}
417
418
419/* initialize driver & display */
420int drv_IMG_init(const char *section, const __attribute__ ((unused))
421     int quiet)
422{
423    int ret;
424
425    info("%s: %s", Name, "$Rev$");
426
427    /* real worker functions */
428    drv_generic_graphic_real_blit = drv_IMG_blit;
429
430    /* start display */
431    if ((ret = drv_IMG_start(section)) != 0)
432  return ret;
433
434    /* initialize generic graphic driver */
435    if ((ret = drv_generic_graphic_init(section, Name)) != 0)
436  return ret;
437
438    /* register plugins */
439    /* none at the moment... */
440
441
442    return 0;
443}
444
445
446/* close driver & display */
447int drv_IMG_quit(const __attribute__ ((unused))
448     int quiet)
449{
450
451    info("%s: shutting down.", Name);
452    drv_generic_graphic_quit();
453
454    if (drv_IMG_FB) {
455  free(drv_IMG_FB);
456  drv_IMG_FB = NULL;
457    }
458
459    return (0);
460}
461
462
463DRIVER drv_Image = {
464    .name = Name,
465    .list = drv_IMG_list,
466    .init = drv_IMG_init,
467    .quit = drv_IMG_quit,
468};
Note: See TracBrowser for help on using the browser.