1
/* $Id: drv_Image.c 840 2007-09-09 12:17:42Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_Image.c $
4
* new style Image (PPM/PNG) Driver for LCD4Linux
6
* Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
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)
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.
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.
31
* struct DRIVER drv_Image
56
#error "gd.h not found!"
57
#error "cannot compile PNG driver"
62
#error "lcd4linux requires libgd version 2"
63
#error "cannot compile PNG driver"
75
#include "drv_generic_graphic.h"
81
static char Name[] = "Image";
83
static enum { NIL, PPM, PNG } Format;
85
static int pixel = -1; /* pointsize in pixel */
86
static int pgap = 0; /* gap between points */
87
static int rgap = 0; /* row gap between lines */
88
static int cgap = 0; /* column gap between characters */
89
static int border = 0; /* window border */
91
static int dimx, dimy; /* total window dimension in pixel */
94
static RGBA *drv_IMG_FB = NULL;
98
/****************************************/
99
/*** hardware dependant functions ***/
100
/****************************************/
103
static int drv_IMG_flush_PPM(void)
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];
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;
115
if (bitbuf == NULL) {
116
if ((bitbuf = malloc(xsize * ysize * sizeof(*bitbuf))) == NULL) {
117
error("%s: malloc() failed: %s", Name, strerror(errno));
122
if (rowbuf == NULL) {
123
if ((rowbuf = malloc(3 * xsize * sizeof(*rowbuf))) == NULL) {
124
error("Raster: malloc() failed: %s", strerror(errno));
129
for (i = 0; i < xsize * ysize; i++) {
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);
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];
144
snprintf(path, sizeof(path), output, seq++);
145
qprintf(tmp, sizeof(tmp), "%s.tmp", path);
147
/* remove the file */
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));
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));
164
for (row = 0; row < ysize; row++) {
166
for (col = 0; col < xsize; col++) {
167
RGBA p = bitbuf[row * xsize + col];
172
if (write(fd, rowbuf, c) < 0) {
173
error("%s: write(%s) failed: %s", Name, tmp, strerror(errno));
179
error("%s: close(%s) failed: %s", Name, tmp, strerror(errno));
182
if (rename(tmp, path) < 0) {
183
error("%s: rename(%s) failed: %s", Name, tmp, strerror(errno));
192
static int drv_IMG_flush_PNG(void)
195
int xsize, ysize, row, col;
196
char path[256], tmp[256];
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;
204
im = gdImageCreateTrueColor(xsize, ysize);
205
gdImageFilledRectangle(im, 0, 0, xsize, ysize, gdTrueColor(BC.R, BC.G, BC.B));
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);
217
snprintf(path, sizeof(path), output, seq++);
218
qprintf(tmp, sizeof(tmp), "%s.tmp", path);
220
/* remove the file */
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));
231
if ((fp = fdopen(fd, "w")) == NULL) {
232
error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno));
241
if (fclose(fp) != 0) {
242
error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno));
246
if (rename(tmp, path) < 0) {
247
error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno));
256
static void drv_IMG_flush(void)
275
static void drv_IMG_timer( __attribute__ ((unused))
285
static void drv_IMG_blit(const int row, const int col, const int height, const int width)
289
for (r = row; r < row + height; r++) {
290
for (c = col; c < col + width; c++) {
291
RGBA p1 = drv_IMG_FB[r * DCOLS + c];
292
RGBA p2 = drv_generic_graphic_rgb(r, c);
293
if (p1.R != p2.R || p1.G != p2.G || p1.B != p2.B) {
294
drv_IMG_FB[r * DCOLS + c] = p2;
302
static int drv_IMG_start(const char *section)
307
if (output == NULL || *output == '\0') {
308
error("%s: no output file specified (use -o switch)", Name);
312
/* read file format from config */
313
s = cfg_get(section, "Format", NULL);
314
if (s == NULL || *s == '\0') {
315
error("%s: no '%s.Format' entry from %s", Name, section, cfg_source());
323
if (strcmp(s, "PPM") == 0) {
329
if (strcmp(s, "PNG") == 0) {
335
error("%s: bad %s.Format '%s' from %s", Name, section, s, cfg_source());
341
/* read display size from config */
342
if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
343
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
349
if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
350
error("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source());
356
if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) {
357
error("%s: bad %s.Pixel '%s' from %s", Name, section, s, cfg_source());
363
if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) {
364
error("%s: bad %s.Gap '%s' from %s", Name, section, s, cfg_source());
375
if (cfg_number(section, "border", 0, 0, -1, &border) < 0)
378
s = cfg_get(section, "basecolor", "000000ff");
379
if (color2RGBA(s, &BC) < 0) {
380
error("%s: ignoring illegal color '%s'", Name, s);
384
drv_IMG_FB = malloc(DCOLS * DROWS * sizeof(*drv_IMG_FB));
385
if (drv_IMG_FB == NULL) {
386
error("%s: framebuffer could not be allocated: malloc() failed", Name);
390
for (i = 0; i < DCOLS * DROWS; i++) {
394
dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap;
395
dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap;
398
/* initially flush the image to a file */
401
/* regularly flush the image to a file */
402
/* Fixme: make 100msec configurable */
403
timer_add(drv_IMG_timer, NULL, 100, 0);
411
/****************************************/
413
/****************************************/
415
/* none at the moment... */
418
/****************************************/
419
/*** exported functions ***/
420
/****************************************/
424
int drv_IMG_list(void)
436
/* initialize driver & display */
437
int drv_IMG_init(const char *section, const __attribute__ ((unused))
442
info("%s: %s", Name, "$Rev: 840 $");
444
/* real worker functions */
445
drv_generic_graphic_real_blit = drv_IMG_blit;
448
if ((ret = drv_IMG_start(section)) != 0)
451
/* initialize generic graphic driver */
452
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
455
/* register plugins */
456
/* none at the moment... */
463
/* close driver & display */
464
int drv_IMG_quit(const __attribute__ ((unused))
468
info("%s: shutting down.", Name);
469
drv_generic_graphic_quit();
482
.list = drv_IMG_list,
483
.init = drv_IMG_init,
484
.quit = drv_IMG_quit,