glcompimage.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <glcomp/glcompimage.h>
  11. #include <glcomp/glcompfont.h>
  12. #include <glcomp/glcompset.h>
  13. #include <glcomp/glutils.h>
  14. #include <glcomp/glcomptexture.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <util/alloc.h>
  18. glCompImage *glCompImageNew(void *par, float x, float y) {
  19. glCompImage *p = gv_alloc(sizeof(glCompImage));
  20. glCompInitCommon(&p->base, par, x, y);
  21. p->texture = NULL;
  22. p->base.common.functions.draw = glCompImageDraw;
  23. return p;
  24. }
  25. /* glCompImageNewFile:
  26. * Creates image from given input file.
  27. * At present, we assume png input.
  28. * Return 0 on failure.
  29. */
  30. glCompImage *glCompImageNewFile(float x, float y, const char *imgfile) {
  31. int imageWidth, imageHeight;
  32. cairo_surface_t *surface = NULL;
  33. const unsigned char *data = glCompLoadPng(&surface, imgfile, &imageWidth,
  34. &imageHeight);
  35. glCompImage *p;
  36. if (!data) return NULL;
  37. p = glCompImageNew(NULL, x, y);
  38. if (!glCompImageLoad(p, data, imageWidth, imageHeight, 0)) {
  39. glCompImageDelete (p);
  40. p = NULL;
  41. }
  42. cairo_surface_destroy(surface);
  43. return p;
  44. }
  45. void glCompImageDelete(glCompImage * p)
  46. {
  47. glCompEmptyCommon(&p->base.common);
  48. if (p->texture)
  49. glCompDeleteTexture(p->texture);
  50. free(p);
  51. }
  52. int glCompImageLoad(glCompImage *i, const unsigned char *data, int width,
  53. int height, bool is2D) {
  54. if (data != NULL) { /*valid image data */
  55. glCompDeleteTexture(i->texture);
  56. i->texture =
  57. glCompSetAddNewTexImage(i->base.common.compset, width, height, data,
  58. is2D);
  59. if (i->texture) {
  60. i->base.common.width = width;
  61. i->base.common.height = height;
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }
  67. int glCompImageLoadPng(glCompImage *i, const char *pngFile) {
  68. int imageWidth, imageHeight;
  69. cairo_surface_t *surface = NULL;
  70. const unsigned char *data = glCompLoadPng(&surface, pngFile, &imageWidth,
  71. &imageHeight);
  72. const int rc = glCompImageLoad(i, data, imageWidth, imageHeight, 1);
  73. cairo_surface_destroy(surface);
  74. return rc;
  75. }
  76. void glCompImageDraw(void *obj)
  77. {
  78. glCompImage *p = obj;
  79. glCompCommon ref = p->base.common;
  80. float w,h,d;
  81. glCompCalcWidget(p->base.common.parent, &p->base.common, &ref);
  82. if (!p->base.common.visible)
  83. return;
  84. if (!p->texture)
  85. return;
  86. if(p->texture->id <=0)
  87. {
  88. glRasterPos2f(ref.pos.x, ref.pos.y);
  89. glDrawPixels(p->texture->width, p->texture->height, GL_RGBA,GL_UNSIGNED_BYTE, p->texture->data);
  90. }
  91. else
  92. {
  93. w = p->width;
  94. h = p->height;
  95. d = (float)p->base.common.layer * GLCOMPSET_BEVEL_DIFF;
  96. glDisable(GL_BLEND);
  97. glEnable(GL_TEXTURE_2D);
  98. glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  99. glBindTexture(GL_TEXTURE_2D,p->texture->id);
  100. glBegin(GL_QUADS);
  101. glTexCoord2d(0.0f, 1.0f);glVertex3d(ref.pos.x,ref.pos.y,d);
  102. glTexCoord2d(1.0f, 1.0f);glVertex3d(ref.pos.x+w,ref.pos.y,d);
  103. glTexCoord2d(1.0f, 0.0f);glVertex3d(ref.pos.x+w,ref.pos.y+h,d);
  104. glTexCoord2d(0.0f, 0.0f);glVertex3d(ref.pos.x,ref.pos.y+h,d);
  105. glEnd();
  106. glDisable(GL_TEXTURE_2D);
  107. glEnable(GL_BLEND);
  108. }
  109. }