File: sipp_pixmap.c

package info (click to toggle)
sipp 3.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,268 kB
  • ctags: 744
  • sloc: ansic: 8,534; makefile: 304; lex: 65; sh: 14
file content (128 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 ** sipp - SImple Polygon Processor
 **
 **  A general 3d graphic package
 **
 **  Copyright Equivalent Software HB  1992
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation; either version 1, or any later version.
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 ** You can receive a copy of the GNU General Public License from the
 ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **/

/**
 ** sipp_pixmap.c - A sample pixmap for use in SIPP and routines to use it.
 **/

#include <sys/types.h>
#include <stdio.h>

#include <sipp.h>
#include <smalloc.h>
#include <sipp_pixmap.h>


/* ================================================================ */


/*
 * Return a pointer to a new Sipp_pixmap, with the size WIDTH x HEIGHT.
 */

Sipp_pixmap *
sipp_pixmap_create(width, height)
    int   width;
    int   height;
{
    Sipp_pixmap  * pm;

    pm = (Sipp_pixmap *) smalloc(sizeof(Sipp_pixmap));
    pm->width = width;
    pm->height = height;
    pm->buffer = (u_char *) scalloc(3 * width * height, sizeof(u_char));

    return pm;
}



/*
 * Destruct a pixmap, and free allocated memory.
 */

void
sipp_pixmap_destruct(pm)
    Sipp_pixmap  * pm;
{
    if (pm != NULL) {
        if (pm->buffer != NULL) {
            sfree(pm->buffer);
        }
        sfree(pm);
    }
}




/*
 * Set the pixel at (X, Y) in Sipp_pixmap PM to the color
 * (RED, GRN, BLU).  
 */

void
sipp_pixmap_set_pixel(pm, x, y, red, grn, blu)
    Sipp_pixmap  * pm;
    int            x;
    int            y;
    u_char         red;
    u_char         grn;
    u_char         blu;
{
    u_char  * cp;

    if (x < 0 || y < 0 || x >= pm->width || y >= pm->height) {
        fprintf(stderr, 
"Tried to write to pixel (%d, %d) in a Sipp_pixmap which was only %d x %d.\n",
                x, y, pm->width, pm->height);
    } else {
        cp = pm->buffer + 3 * (y * pm->width + x);
        *cp++ = red;
        *cp++ = grn;
        *cp = blu;
    }
}



/*
 * Write the Sipp_pixmap PM to the open file FILE.
 */

void
sipp_pixmap_write(file, pm)
    FILE         * file;
    Sipp_pixmap  * pm;
{
    u_char  * byte;
    int       nbytes;
    int       i;

    fprintf(file,  "P6\n");
    fprintf(file,  "#Image rendered with SIPP %s\n",  SIPP_VERSION);
    fprintf(file,  "%d\n%d\n255\n", pm->width, pm->height);

    byte = pm->buffer;
    nbytes = pm->width * pm->height;
    for (i = 0; i < nbytes; ++i) {
        putc(*byte++, file);
        putc(*byte++, file);
        putc(*byte++, file);
    }
}