| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <glcomp/glcompimage.h> |
| 14 |
#include <glcomp/glcompfont.h> |
| 15 |
#include <glcomp/glcompset.h> |
| 16 |
#include <glcomp/glutils.h> |
| 17 |
#include <glcomp/glcomptexture.h> |
| 18 |
#include <stdbool.h> |
| 19 |
#include <stddef.h> |
| 20 |
#include <util/alloc.h> |
| 21 |
|
| 22 |
glCompImage *glCompImageNew(void *par, float x, float y) { |
| 23 |
glCompImage *p = gv_alloc(sizeof(glCompImage)); |
| 24 |
glCompInitCommon(&p->base, par, x, y); |
| 25 |
p->texture = NULL; |
| 26 |
p->base.common.functions.draw = glCompImageDraw; |
| 27 |
return p; |
| 28 |
} |
| 29 |
|
| 30 |
/* glCompImageNewFile: |
| 31 |
* Creates image from given input file. |
| 32 |
* At present, we assume png input. |
| 33 |
* Return 0 on failure. |
| 34 |
*/ |
| 35 |
glCompImage *glCompImageNewFile(float x, float y, const char *imgfile) { |
| 36 |
int imageWidth, imageHeight; |
| 37 |
cairo_surface_t *surface = NULL; |
| 38 |
const unsigned char *data = glCompLoadPng(&surface, imgfile, &imageWidth, |
| 39 |
&imageHeight); |
| 40 |
glCompImage *p; |
| 41 |
|
| 42 |
if (!data) return NULL; |
| 43 |
p = glCompImageNew(NULL, x, y); |
| 44 |
if (!glCompImageLoad(p, data, imageWidth, imageHeight, 0)) { |
| 45 |
glCompImageDelete (p); |
| 46 |
p = NULL; |
| 47 |
} |
| 48 |
cairo_surface_destroy(surface); |
| 49 |
return p; |
| 50 |
} |
| 51 |
|
| 52 |
void glCompImageDelete(glCompImage * p) |
| 53 |
{ |
| 54 |
glCompEmptyCommon(&p->base.common); |
| 55 |
if (p->texture) |
| 56 |
glCompDeleteTexture(p->texture); |
| 57 |
free(p); |
| 58 |
} |
| 59 |
|
| 60 |
int glCompImageLoad(glCompImage *i, const unsigned char *data, int width, |
| 61 |
int height, bool is2D) { |
| 62 |
if (data != NULL) { /*valid image data */ |
| 63 |
glCompDeleteTexture(i->texture); |
| 64 |
i->texture = |
| 65 |
glCompSetAddNewTexImage(i->base.common.compset, width, height, data, |
| 66 |
is2D); |
| 67 |
if (i->texture) { |
| 68 |
i->base.common.width = width; |
| 69 |
i->base.common.height = height; |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int glCompImageLoadPng(glCompImage *i, const char *pngFile) { |
| 78 |
int imageWidth, imageHeight; |
| 79 |
cairo_surface_t *surface = NULL; |
| 80 |
const unsigned char *data = glCompLoadPng(&surface, pngFile, &imageWidth, |
| 81 |
&imageHeight); |
| 82 |
if (data == NULL) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
const int rc = glCompImageLoad(i, data, imageWidth, imageHeight, 1); |
| 86 |
cairo_surface_destroy(surface); |
| 87 |
return rc; |
| 88 |
} |
| 89 |
|
| 90 |
void glCompImageDraw(void *obj) |
| 91 |
{ |
| 92 |
glCompImage *p = obj; |
| 93 |
glCompCommon ref = p->base.common; |
| 94 |
float w,h,d; |
| 95 |
|
| 96 |
glCompCalcWidget(p->base.common.parent, &p->base.common, &ref); |
| 97 |
if (!p->base.common.visible) |
| 98 |
return; |
| 99 |
if (!p->texture) |
| 100 |
return; |
| 101 |
|
| 102 |
if(p->texture->id <=0) |
| 103 |
{ |
| 104 |
glRasterPos2f(ref.pos.x, ref.pos.y); |
| 105 |
glDrawPixels(p->texture->width, p->texture->height, GL_RGBA,GL_UNSIGNED_BYTE, p->texture->data); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
w = p->width; |
| 110 |
h = p->height; |
| 111 |
d = (float)p->base.common.layer * GLCOMPSET_BEVEL_DIFF; |
| 112 |
glDisable(GL_BLEND); |
| 113 |
glEnable(GL_TEXTURE_2D); |
| 114 |
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 115 |
glBindTexture(GL_TEXTURE_2D,p->texture->id); |
| 116 |
glBegin(GL_QUADS); |
| 117 |
glTexCoord2d(0.0f, 1.0f);glVertex3d(ref.pos.x,ref.pos.y,d); |
| 118 |
glTexCoord2d(1.0f, 1.0f);glVertex3d(ref.pos.x+w,ref.pos.y,d); |
| 119 |
glTexCoord2d(1.0f, 0.0f);glVertex3d(ref.pos.x+w,ref.pos.y+h,d); |
| 120 |
glTexCoord2d(0.0f, 0.0f);glVertex3d(ref.pos.x,ref.pos.y+h,d); |
| 121 |
glEnd(); |
| 122 |
|
| 123 |
|
| 124 |
glDisable(GL_TEXTURE_2D); |
| 125 |
glEnable(GL_BLEND); |
| 126 |
} |
| 127 |
|
| 128 |
} |