/tags/rel-1-3-15/SWIG/Examples/GIFPlot/Lib/frame.c
C | 924 lines | 590 code | 152 blank | 182 comment | 141 complexity | 4e4111073b69a40b55b9b82b914347ab MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
- /* -----------------------------------------------------------------------------
- * frame.c
- *
- * Frame buffer management
- *
- * Author(s) : David Beazley (beazley@cs.uchicago.edu)
- * Copyright (C) 1995-1996
- *
- * See the file LICENSE for information on usage and redistribution.
- * ----------------------------------------------------------------------------- */
- #define FRAME
- #include "gifplot.h"
- #include <float.h>
- /* ------------------------------------------------------------------------
- FrameBuffer *new_FrameBuffer(int width, int height)
- Creates a new framebuffer for storing data.
- ------------------------------------------------------------------------ */
- FrameBuffer *new_FrameBuffer(unsigned int width, unsigned int height) {
- FrameBuffer *f;
- int FrameBuffer_resize(FrameBuffer *f, int width, int height);
- /* Create a new frame buffer */
-
- f = (FrameBuffer *) malloc(sizeof(FrameBuffer));
- f->pixels = (Pixel **) 0;
- f->zbuffer = (Zvalue **) 0;
- /* Set its size */
-
- if (FrameBuffer_resize(f, width, height) == -1) {
- free((char *) f);
- return (FrameBuffer *) 0;
- }
- f->xmin = 0;
- f->ymin = 0;
- f->xmax = width;
- f->ymax = height;
- return f;
- }
- /* ------------------------------------------------------------------------
- void delete_FrameBuffer(FrameBuffer *f)
- Destroys the given framebuffer
- ------------------------------------------------------------------------ */
- void delete_FrameBuffer(FrameBuffer *f) {
- if (f) {
- if (f->pixels) {
- free((char *) f->pixels[0]);
- free((char *) f->pixels);
- }
- if (f->zbuffer) {
- free((char *) f->zbuffer[0]);
- free((char *) f->zbuffer);
- }
- free((char *)f);
- }
- }
- /* ------------------------------------------------------------------------
- int *FrameBuffer_resize(FrameBuffer *f, int width, int height)
- Resize the given framebuffer. Returns 0 on success, -1 on failure.
- ------------------------------------------------------------------------ */
- int FrameBuffer_resize(FrameBuffer *f, int width, int height) {
- int i;
- if ((f) && (width > 0) && (height > 0)) {
- if (f->pixels) {
- free((char *)f->pixels[0]);
- free((char *)f->pixels);
- }
- f->pixels = (Pixel **) malloc (height*sizeof(Pixel *));
- if (!f->pixels) return -1;
- f->pixels[0] = (Pixel *) malloc(height*width*sizeof(Pixel));
- if (!f->pixels[0]) {
- free((char *)f->pixels);
- return -1;
- }
- for (i = 0; i < height; i++)
- f->pixels[i] = f->pixels[0] + i*width;
- f->width = width;
- f->height = height;
- if (f->zbuffer) {
- FrameBuffer_zresize(f,width,height);
- }
- return 0;
- } else {
- return -1;
- }
- }
- /* ------------------------------------------------------------------------
- void FrameBuffer_clear(FrameBuffer *f, Pixel color)
- Clears the current FrameBuffer
- ------------------------------------------------------------------------ */
- void FrameBuffer_clear(FrameBuffer *f, Pixel color) {
- Pixel *p;
- unsigned int i;
- p = &f->pixels[0][0];
- for (i = 0; i < f->width*f->height; i++, p++)
- *p = color;
- }
- /* ------------------------------------------------------------------------
- void FrameBuffer_plot(FrameBuffer *f, int x1, int y1, Pixel color)
- Plots a point and does a bounds check.
- ------------------------------------------------------------------------ */
- void FrameBuffer_plot(FrameBuffer *f, int x1, int y1, Pixel color) {
- if ((x1 < f->xmin) || (x1 >= f->xmax) || (y1 < f->ymin) || (y1 >= f->ymax))
- return;
- f->pixels[y1][x1] = color;
- }
- /* ------------------------------------------------------------------------
- FrameBuffer_horizontal(Framebuffer *f, int xmin, int xmax, int y, Pixel color)
- Draw a horizontal line (clipped)
- ------------------------------------------------------------------------ */
- void FrameBuffer_horizontal(FrameBuffer *f, int xmin, int xmax, int y, Pixel color) {
-
- Pixel *p;
- int i;
-
- if ((y < f->ymin) || (y >= f->ymax)) return;
- if (xmin < f->xmin) xmin = f->xmin;
- if (xmax >= f->xmax) xmax = f->xmax - 1;
- p = &f->pixels[y][xmin];
- for (i = xmin; i <= xmax; i++, p++)
- *p = color;
- }
- /* ------------------------------------------------------------------------
- FrameBuffer_horizontalinterp(Framebuffer *f, int xmin, int xmax, int y,
- Pixel c1, Pixel c2)
- Draw a horizontal line (clipped) with color interpolation.
- ------------------------------------------------------------------------ */
- void FrameBuffer_horizontalinterp(FrameBuffer *f, int xmin, int xmax, int y,
- Pixel c1, Pixel c2) {
-
- Pixel *p;
- int i;
- double mc;
- int x1;
- if ((y < f->ymin) || (y >= f->ymax)) return;
- x1 = xmin;
- if (xmin < f->xmin) xmin = f->xmin;
- if (xmax >= f->xmax) xmax = f->xmax - 1;
- if (xmax < f->xmin) return;
- if (xmin >= f->xmax) return;
- if (xmin != xmax)
- mc = (double)(c2 - c1)/(double) (xmax - xmin);
- else
- mc = 0.0;
- p = &f->pixels[y][xmin];
- for (i = xmin; i <= xmax; i++, p++)
- *p = (Pixel) (mc*(i-x1) + c1);
-
- }
- /* ------------------------------------------------------------------------
- FrameBuffer_vertical(Framebuffer *f, int xmin, int xmax, int y, Pixel color)
- Draw a Vertical line (clipped)
- ------------------------------------------------------------------------ */
- void FrameBuffer_vertical(FrameBuffer *f, int ymin, int ymax, int x, Pixel color) {
-
- Pixel *p;
- int i;
-
- if ((x < f->xmin) || (x >= f->xmax)) return;
- if (ymax < f->ymin) return;
- if (ymin > f->ymax) return;
- if (ymin < f->ymin) ymin = f->ymin;
- if (ymax >= f->ymax) ymax = f->ymax - 1;
- p = &f->pixels[ymin][x];
- for (i = 0; i <= (ymax - ymin); i++, p+=f->width)
- *p = color;
- }
-
- /* ------------------------------------------------------------------------
- void FrameBuffer_box(FrameBuffer *f, int x1, int y1, int x2, int y2, Pixel color)
- Makes an outline box.
- ------------------------------------------------------------------------ */
- void FrameBuffer_box(FrameBuffer *f, int x1, int y1, int x2, int y2, Pixel color) {
- int xt, yt;
- /* Make sure points are in correct order */
- if (x2 < x1) {
- xt = x2;
- x2 = x1;
- x1 = xt;
- }
- if (y2 < y1) {
- yt = y2;
- y2 = y1;
- y1 = yt;
- }
- /* Draw lower edge */
- FrameBuffer_horizontal(f,x1,x2,y1,color);
- /* Draw upper edge */
- FrameBuffer_horizontal(f,x1,x2,y2,color);
- /* Draw left side */
- FrameBuffer_vertical(f,y1,y2,x1,color);
- /* Draw right side */
- FrameBuffer_vertical(f,y1,y2,x2,color);
-
- }