Graphviz 13.0.0~dev.20250511.0440
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 if (data == NULL) {
81 return 0;
82 }
83 const int rc = glCompImageLoad(i, data, imageWidth, imageHeight, 1);
84 cairo_surface_destroy(surface);
85 return rc;
86}
87
88void glCompImageDraw(void *obj)
89{
90 glCompImage *p = obj;
92 float w,h,d;
93
95 if (!p->base.common.visible)
96 return;
97 if (!p->texture)
98 return;
99
100 if(p->texture->id <=0)
101 {
102 glRasterPos2f(ref.pos.x, ref.pos.y);
103 glDrawPixels(p->texture->width, p->texture->height, GL_RGBA,GL_UNSIGNED_BYTE, p->texture->data);
104 }
105 else
106 {
107 w = p->width;
108 h = p->height;
109 d = (float)p->base.common.layer * GLCOMPSET_BEVEL_DIFF;
110 glDisable(GL_BLEND);
111 glEnable(GL_TEXTURE_2D);
112 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
113 glBindTexture(GL_TEXTURE_2D,p->texture->id);
114 glBegin(GL_QUADS);
115 glTexCoord2d(0.0f, 1.0f);glVertex3d(ref.pos.x,ref.pos.y,d);
116 glTexCoord2d(1.0f, 1.0f);glVertex3d(ref.pos.x+w,ref.pos.y,d);
117 glTexCoord2d(1.0f, 0.0f);glVertex3d(ref.pos.x+w,ref.pos.y+h,d);
118 glTexCoord2d(0.0f, 0.0f);glVertex3d(ref.pos.x,ref.pos.y+h,d);
119 glEnd();
120
121
122 glDisable(GL_TEXTURE_2D);
123 glEnable(GL_BLEND);
124 }
125
126}
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:88
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:110
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:165
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:150
glCompCallBacks functions
Definition glcompdefs.h:179
void * parent
Definition glcompdefs.h:172
glCompSet * compset
compset
Definition glcompdefs.h:171
glCompObj base
Definition glcompdefs.h:190
glCompTex * texture
Definition glcompdefs.h:191
glCompCommon common
Definition glcompdefs.h:185
uint32_t id
Definition glcompdefs.h:124
unsigned char * data
Definition glcompdefs.h:132