Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
glcompimage.c
Go to the documentation of this file.
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 <glcomp/glcompimage.h>
12#include <glcomp/glcompfont.h>
13#include <glcomp/glcompset.h>
14#include <glcomp/glutils.h>
16#include <stdbool.h>
17#include <stddef.h>
18#include <util/alloc.h>
19
20glCompImage *glCompImageNew(void *par, float x, float y) {
21 glCompImage *p = gv_alloc(sizeof(glCompImage));
22 glCompInitCommon(&p->base, par, x, y);
23 p->texture = NULL;
25 return p;
26}
27
28/* glCompImageNewFile:
29 * Creates image from given input file.
30 * At present, we assume png input.
31 * Return 0 on failure.
32 */
33glCompImage *glCompImageNewFile(float x, float y, const char *imgfile) {
34 int imageWidth, imageHeight;
35 cairo_surface_t *surface = NULL;
36 const unsigned char *data = glCompLoadPng(&surface, imgfile, &imageWidth,
37 &imageHeight);
38 glCompImage *p;
39
40 if (!data) return NULL;
41 p = glCompImageNew(NULL, x, y);
42 if (!glCompImageLoad(p, data, imageWidth, imageHeight, 0)) {
44 p = NULL;
45 }
46 cairo_surface_destroy(surface);
47 return p;
48}
49
57
58int glCompImageLoad(glCompImage *i, const unsigned char *data, int width,
59 int height, bool is2D) {
60 if (data != NULL) { /*valid image data */
62 i->texture =
64 is2D);
65 if (i->texture) {
66 i->base.common.width = width;
67 i->base.common.height = height;
68 return 1;
69 }
70
71 }
72 return 0;
73}
74
75int glCompImageLoadPng(glCompImage *i, const char *pngFile) {
76 int imageWidth, imageHeight;
77 cairo_surface_t *surface = NULL;
78 const unsigned char *data = glCompLoadPng(&surface, pngFile, &imageWidth,
79 &imageHeight);
80 const int rc = glCompImageLoad(i, data, imageWidth, imageHeight, 1);
81 cairo_surface_destroy(surface);
82 return rc;
83}
84
85void glCompImageDraw(void *obj)
86{
87 glCompImage *p = obj;
89 float w,h,d;
90
92 if (!p->base.common.visible)
93 return;
94 if (!p->texture)
95 return;
96
97 if(p->texture->id <=0)
98 {
99 glRasterPos2f(ref.pos.x, ref.pos.y);
100 glDrawPixels(p->texture->width, p->texture->height, GL_RGBA,GL_UNSIGNED_BYTE, p->texture->data);
101 }
102 else
103 {
104 w = p->width;
105 h = p->height;
106 d = (float)p->base.common.layer * GLCOMPSET_BEVEL_DIFF;
107 glDisable(GL_BLEND);
108 glEnable(GL_TEXTURE_2D);
109 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
110 glBindTexture(GL_TEXTURE_2D,p->texture->id);
111 glBegin(GL_QUADS);
112 glTexCoord2d(0.0f, 1.0f);glVertex3d(ref.pos.x,ref.pos.y,d);
113 glTexCoord2d(1.0f, 1.0f);glVertex3d(ref.pos.x+w,ref.pos.y,d);
114 glTexCoord2d(1.0f, 0.0f);glVertex3d(ref.pos.x+w,ref.pos.y+h,d);
115 glTexCoord2d(0.0f, 0.0f);glVertex3d(ref.pos.x,ref.pos.y+h,d);
116 glEnd();
117
118
119 glDisable(GL_TEXTURE_2D);
120 glEnable(GL_BLEND);
121 }
122
123}
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define GLCOMPSET_BEVEL_DIFF
Definition glcompdefs.h:56
void glCompImageDelete(glCompImage *p)
Definition glcompimage.c:50
int glCompImageLoadPng(glCompImage *i, const char *pngFile)
Definition glcompimage.c:75
int glCompImageLoad(glCompImage *i, const unsigned char *data, int width, int height, bool is2D)
Definition glcompimage.c:58
void glCompImageDraw(void *obj)
Definition glcompimage.c:85
glCompImage * glCompImageNew(void *par, float x, float y)
Definition glcompimage.c:20
glCompImage * glCompImageNewFile(float x, float y, const char *imgfile)
Definition glcompimage.c:33
void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x, float y)
Definition glcompset.c:114
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:177
unsigned char * glCompLoadPng(cairo_surface_t **surface, const char *filename, int *imageWidth, int *imageHeight)
void glCompDeleteTexture(glCompTex *t)
glCompTex * glCompSetAddNewTexImage(glCompSet *s, int width, int height, const unsigned char *data, bool is2D)
void glCompCalcWidget(glCompCommon *parent, glCompCommon *child, glCompCommon *ref)
Definition glutils.c:195
void free(void *)
node NULL
Definition grammar.y:163
void ref(Site *v)
Definition site.c:59
Definition legal.c:50
glcompdrawfunc_t draw
Definition glcompdefs.h:160
glCompCallBacks functions
Definition glcompdefs.h:194
void * parent
Definition glcompdefs.h:187
glCompSet * compset
compset
Definition glcompdefs.h:186
glCompObj base
Definition glcompdefs.h:205
glCompTex * texture
Definition glcompdefs.h:206
glCompCommon common
Definition glcompdefs.h:200
uint32_t id
Definition glcompdefs.h:134
unsigned char * data
Definition glcompdefs.h:142