aboutsummaryrefslogtreecommitdiffstats
path: root/widget_image.c
diff options
context:
space:
mode:
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2006-02-19 07:20:54 +0000
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2006-02-19 07:20:54 +0000
commit55a19a104867be10dfa251d363debad53af73cb9 (patch)
tree19da79cc03adc3dfcf4cd2431b2d16ea883a1a79 /widget_image.c
parentf320392043151ca49dde0b3a2e08617ae02ebd53 (diff)
downloadlcd4linux-55a19a104867be10dfa251d363debad53af73cb9.tar.gz
[lcd4linux @ 2006-02-19 07:20:53 by reinelt]
image support nearly finished git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@638 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'widget_image.c')
-rw-r--r--widget_image.c96
1 files changed, 87 insertions, 9 deletions
diff --git a/widget_image.c b/widget_image.c
index 6c510e7..1cd17f6 100644
--- a/widget_image.c
+++ b/widget_image.c
@@ -1,4 +1,4 @@
-/* $Id: widget_image.c,v 1.3 2006/02/08 04:55:05 reinelt Exp $
+/* $Id: widget_image.c,v 1.4 2006/02/19 07:20:54 reinelt Exp $
*
* image widget handling
*
@@ -21,6 +21,9 @@
*
*
* $Log: widget_image.c,v $
+ * Revision 1.4 2006/02/19 07:20:54 reinelt
+ * image support nearly finished
+ *
* Revision 1.3 2006/02/08 04:55:05 reinelt
* moved widget registration to drv_generic_graphic
*
@@ -47,6 +50,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <errno.h>
#include "debug.h"
#include "cfg.h"
@@ -55,6 +59,7 @@
#include "timer.h"
#include "widget.h"
#include "widget_image.h"
+#include "rgb.h"
#ifdef HAVE_GD_GD_H
#include <gd/gd.h>
@@ -70,13 +75,85 @@
#include <dmalloc.h>
#endif
-static void widget_image_render (WIDGET_IMAGE *Image)
-{
- /* try to open file */
+#ifdef WITH_GD
+static void widget_image_render(const char *Name, WIDGET_IMAGE * Image)
+{
+ int x, y;
+ FILE *fd;
+ gdImagePtr gdImage = NULL;
+
+ /* clear bitmap */
+ if (Image->bitmap) {
+ int i;
+ for (i = 0; i < Image->height * Image->width; i++) {
+ RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 };
+ Image->bitmap[i] = empty;
+ }
+ }
+
if (Image->file == NULL || Image->file[0] == '\0') {
+ error("Warning: Image %s has no file", Name);
+ return;
+ }
+
+ fd = fopen(Image->file, "rb");
+ if (fd == NULL) {
+ error("Warning: Image %s: fopen(%s) failed: %s", Name, Image->file, strerror(errno));
+ return;
+ }
+
+ gdImage = gdImageCreateFromPng(fd);
+ fclose(fd);
+
+ if (fd == NULL) {
+ error("Warning: Image %s: CreateFromPng(%s) failed!", Name, Image->file);
+ return;
+ }
+
+ /* maybe resize bitmap */
+ if (gdImage->sx > Image->width) {
+ Image->width = gdImage->sx;
+ if (Image->bitmap)
+ free(Image->bitmap);
+ Image->bitmap = NULL;
+ }
+ if (gdImage->sy > Image->height) {
+ Image->height = gdImage->sy;
+ if (Image->bitmap)
+ free(Image->bitmap);
+ Image->bitmap = NULL;
+ }
+ if (Image->bitmap == NULL && Image->width > 0 && Image->height > 0) {
+ int i = Image->width * Image->height * sizeof(Image->bitmap[0]);
+ Image->bitmap = malloc(i);
+ if (Image->bitmap == NULL) {
+ error("Warning: Image %s: malloc(%d) failed!", Name, i, strerror(errno));
+ return;
+ }
+ for (i = 0; i < Image->height * Image->width; i++) {
+ RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 };
+ Image->bitmap[i] = empty;
+ }
+ }
+
+ /* finally really render it */
+ for (x=0; x < gdImage->sx; x++) {
+ for (y=0; y < gdImage->sy; y++) {
+ int p = gdImageGetTrueColorPixel (gdImage, x, y);
+ int a = gdTrueColorGetAlpha(p);
+ int i = y*Image->width+x;
+ Image->bitmap[i].R = gdTrueColorGetRed(p);
+ Image->bitmap[i].G = gdTrueColorGetGreen (p);
+ Image->bitmap[i].B = gdTrueColorGetBlue(p);
+ /* GD's alpha is 0 (opaque) to 127 (tranparanet) */
+ /* our alpha is 0 (transparent) to 255 (opaque) */
+ Image->bitmap[i].A = (a == 127) ? 0 : 255-2*a;
+ }
}
}
+#endif
+
static void widget_image_update(void *Self)
{
@@ -89,7 +166,7 @@ static void widget_image_update(void *Self)
/* evaluate expressions */
if (Image->file) {
- free (Image->file);
+ free(Image->file);
Image->file = NULL;
}
if (Image->file_tree != NULL) {
@@ -115,12 +192,13 @@ static void widget_image_update(void *Self)
Image->visible = 0;
DelResult(&result);
}
-
+
+#ifdef WITH_GD
/* render image into bitmap */
- widget_image_render(Image);
- }
+ widget_image_render(W->name, Image);
+#endif
- error ("Fixme: We are at image_update");
+ }
/* finally, draw it! */
if (W->class->draw)