gvdevice_devil.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "config.h"
  11. #include <gvc/gvplugin_device.h>
  12. #include <IL/il.h>
  13. #include <IL/ilu.h>
  14. static void
  15. Y_inv ( unsigned int width, unsigned int height, char *data)
  16. {
  17. unsigned int x, y, *a, *b, t;
  18. a = (unsigned int*)data;
  19. b = a + (height-1) * width;
  20. for (y = 0; y < height/2; y++) {
  21. for (x = 0; x < width; x++) {
  22. t = *a;
  23. *a++ = *b;
  24. *b++ = t;
  25. }
  26. b -= 2*width;
  27. }
  28. }
  29. static void devil_format(GVJ_t * job)
  30. {
  31. ILuint ImgId;
  32. // Check if the shared lib's version matches the executable's version.
  33. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
  34. iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION) {
  35. fprintf(stderr, "DevIL version is different...exiting!\n");
  36. }
  37. // Initialize DevIL.
  38. ilInit();
  39. // Generate the main image name to use.
  40. ilGenImages(1, &ImgId);
  41. // Bind this image name.
  42. ilBindImage(ImgId);
  43. // cairo's in-memory image format needs inverting for DevIL
  44. Y_inv ( job->width, job->height, job->imagedata );
  45. // let the DevIL do its thing
  46. (void)ilTexImage(job->width, job->height,
  47. 1, // Depth
  48. 4, // Bpp
  49. IL_BGRA, // Format
  50. IL_UNSIGNED_BYTE,// Type
  51. job->imagedata);
  52. // check the last step succeeded
  53. ILenum Error = ilGetError();
  54. if (Error == 0) {
  55. // output to the provided open file handle
  56. ilSaveF((ILenum)job->device.id, job->output_file);
  57. } else {
  58. fprintf(stderr, "Error: %s\n", iluErrorString(Error));
  59. }
  60. // We're done with the image, so delete it.
  61. ilDeleteImages(1, &ImgId);
  62. // Simple Error detection loop that displays the Error to the user in a human-readable form.
  63. while ((Error = ilGetError())) {
  64. fprintf(stderr, "Error: %s\n", iluErrorString(Error));
  65. }
  66. }
  67. static gvdevice_engine_t devil_engine = {
  68. NULL, /* devil_initialize */
  69. devil_format,
  70. NULL, /* devil_finalize */
  71. };
  72. static gvdevice_features_t device_features_devil = {
  73. GVDEVICE_BINARY_FORMAT
  74. | GVDEVICE_NO_WRITER
  75. | GVDEVICE_DOES_TRUECOLOR,/* flags */
  76. {0.,0.}, /* default margin - points */
  77. {0.,0.}, /* default page width, height - points */
  78. {96.,96.}, /* svg 72 dpi */
  79. };
  80. gvplugin_installed_t gvdevice_devil_types[] = {
  81. {IL_BMP, "bmp:cairo", -1, &devil_engine, &device_features_devil},
  82. {IL_JPG, "jpg:cairo", -1, &devil_engine, &device_features_devil},
  83. {IL_JPG, "jpe:cairo", -1, &devil_engine, &device_features_devil},
  84. {IL_JPG, "jpeg:cairo", -1, &devil_engine, &device_features_devil},
  85. {IL_PNG, "png:cairo", -1, &devil_engine, &device_features_devil},
  86. {IL_TIF, "tif:cairo", -1, &devil_engine, &device_features_devil},
  87. {IL_TIF, "tiff:cairo", -1, &devil_engine, &device_features_devil},
  88. {IL_TGA, "tga:cairo", -1, &devil_engine, &device_features_devil},
  89. {0, NULL, 0, NULL, NULL}
  90. };