Graphviz 14.1.3~dev.20260124.0732
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 "config.h"
12
13#include <glcomp/glcompimage.h>
14#include <glcomp/glcompfont.h>
15#include <glcomp/glcompset.h>
16#include <glcomp/glutils.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <util/alloc.h>
21
22glCompImage *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;
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 */
35glCompImage *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)) {
46 p = NULL;
47 }
48 cairo_surface_destroy(surface);
49 return p;
50}
51
59
60int glCompImageLoad(glCompImage *i, const unsigned char *data, int width,
61 int height, bool is2D) {
62 if (data != NULL) { /*valid image data */
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
77int 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
90void glCompImageDraw(void *obj)
91{
92 glCompImage *p = obj;
93 glCompCommon ref = p->base.common;
94 float w,h,d;
95
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}
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:52
int glCompImageLoadPng(glCompImage *i, const char *pngFile)
Definition glcompimage.c:77
int glCompImageLoad(glCompImage *i, const unsigned char *data, int width, int height, bool is2D)
Definition glcompimage.c:60
void glCompImageDraw(void *obj)
Definition glcompimage.c:90
glCompImage * glCompImageNew(void *par, float x, float y)
Definition glcompimage.c:22
glCompImage * glCompImageNewFile(float x, float y, const char *imgfile)
Definition glcompimage.c:35
void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x, float y)
Definition glcompset.c:113
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:168
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:183
void free(void *)
node NULL
Definition grammar.y:181
glcompdrawfunc_t draw
Definition glcompdefs.h:150
glCompCallBacks functions
Definition glcompdefs.h:179
glCompPoint pos
Definition glcompdefs.h:164
void * parent
Definition glcompdefs.h:172
glCompSet * 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