/tags/rel-1.3.32/Examples/GIFPlot/Lib/pixmap.c

# · C · 159 lines · 102 code · 22 blank · 35 comment · 14 complexity · 85fd84860c04aaba20171c494cda13af MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * pixmap.c
  3. *
  4. * Pixel maps (i.e., bitmaps)
  5. *
  6. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  7. * Copyright (C) 1995-1996
  8. *
  9. * See the file LICENSE for information on usage and redistribution.
  10. * ----------------------------------------------------------------------------- */
  11. #define PIXMAP
  12. #include "gifplot.h"
  13. /* -----------------------------------------------------------------------
  14. PixMap *new_PixMap(int width, int height, int centerx, int centery)
  15. Create a new pixmap of given size
  16. ----------------------------------------------------------------------- */
  17. PixMap *new_PixMap(int width, int height, int centerx, int centery) {
  18. PixMap *pm;
  19. if ((width > 0) && (height > 0)) {
  20. pm = (PixMap *) malloc(sizeof(PixMap));
  21. pm->width = width;
  22. pm->height = height;
  23. pm->centerx = centerx;
  24. pm->centery = centery;
  25. pm->map = (int *) malloc(height*width*sizeof(int));
  26. return pm;
  27. }
  28. return (PixMap *) 0;
  29. }
  30. /* --------------------------------------------------------------------------
  31. void delete_PixMap(PixMap *pm)
  32. Destroy a pixmap
  33. -------------------------------------------------------------------------- */
  34. void delete_PixMap(PixMap *pm) {
  35. if (pm) {
  36. free((char *) pm->map);
  37. free((char *) pm);
  38. }
  39. }
  40. /* ---------------------------------------------------------------------------
  41. void PixMap_set(PixMap *pm, int x, int y, int pix)
  42. Set a pixel in the bitmap
  43. --------------------------------------------------------------------------- */
  44. void
  45. PixMap_set(PixMap *pm, int x, int y, int pix) {
  46. if ((x < 0) || (x>=pm->width)) return;
  47. if ((y < 0) || (y>=pm->height)) return;
  48. pm->map[pm->width*y + x] = pix;
  49. }
  50. /* -----------------------------------------------------------------------------
  51. void FrameBuffer_drawpixmap(FrameBuffer *f, PixMap *pm, int x, int y, int fgcolor, int bgcolor)
  52. Draw a pixmap onto the framebuffer. This is somewhat optimized for speed.
  53. ------------------------------------------------------------------------------ */
  54. void
  55. FrameBuffer_drawpixmap(FrameBuffer *f, PixMap *pm, int x, int y, int fgcolor, int bgcolor) {
  56. int startx, starty; /* Starting location on framebuffer */
  57. int startpixx = 0, startpixy = 0; /* Starting location in pixmap */
  58. int endx, endy; /* Ending location on framebuffer */
  59. int i,j, px, py;
  60. int c;
  61. startx = x - pm->centerx;
  62. starty = y + pm->centery;
  63. endx = startx + pm->width;
  64. endy = starty - pm->height;
  65. /* Figure out if we need to clip */
  66. if (startx < f->xmin) {
  67. startpixx = f->xmin - startx;
  68. startx = f->xmin;
  69. }
  70. if (starty >= f->ymax) {
  71. startpixy = starty - f->ymax;
  72. starty = f->ymax-1;
  73. }
  74. if (endx >= f->xmax) {
  75. endx = f->xmax-1;
  76. }
  77. if (endy < f->ymin) {
  78. endy = f->ymin;
  79. }
  80. py = startpixy;
  81. for (j = starty; j >= endy; j--) {
  82. px = startpixx;
  83. for (i = startx; i < endx; i++) {
  84. c = pm->map[py*pm->width + px];
  85. switch (c) {
  86. case GIFPLOT_FOREGROUND:
  87. f->pixels[j][i] = fgcolor;
  88. break;
  89. case GIFPLOT_BACKGROUND:
  90. f->pixels[j][i] = bgcolor;
  91. break;
  92. default:
  93. break;
  94. }
  95. px++;
  96. }
  97. py++;
  98. }
  99. }
  100. /**************************************************************************
  101. * Some common PixMaps (for plotting)
  102. *
  103. **************************************************************************/
  104. int _SQUARE_MAP[] = {
  105. 0,1,1,1,1,1,1,1,
  106. 0,1,1,1,1,1,1,1,
  107. 0,1,1,1,1,1,1,1,
  108. 0,1,1,1,1,1,1,1,
  109. 0,1,1,1,1,1,1,1,
  110. 0,1,1,1,1,1,1,1,
  111. 0,1,1,1,1,1,1,1,
  112. 0,0,0,0,0,0,0,0 };
  113. PixMap PixMap_SQUARE = { 8,8,4,4, _SQUARE_MAP};
  114. int _TRIANGLE_MAP[] = {
  115. 0,0,0,1,0,0,0,0,
  116. 0,0,0,1,0,0,0,0,
  117. 0,0,1,1,1,0,0,0,
  118. 0,0,1,1,1,0,0,0,
  119. 0,1,1,1,1,1,0,0,
  120. 0,1,1,1,1,1,0,0,
  121. 1,1,1,1,1,1,1,0,
  122. 0,0,0,0,0,0,0,0 };
  123. PixMap PixMap_TRIANGLE = { 8,8,4,4,_TRIANGLE_MAP};
  124. int _CROSS_MAP[] = {
  125. 0,0,0,1,0,0,0,0,
  126. 0,0,0,1,0,0,0,0,
  127. 0,0,0,1,0,0,0,0,
  128. 1,1,1,1,1,1,1,0,
  129. 0,0,0,1,0,0,0,0,
  130. 0,0,0,1,0,0,0,0,
  131. 0,0,0,1,0,0,0,0,
  132. 0,0,0,0,0,0,0,0 };
  133. PixMap PixMap_CROSS = { 8,8,4,4,_CROSS_MAP};