2
* Copyright (c) 2006 Ondrej Palkovsky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
#include <sys/types.h>
34
static void skip_whitespace(unsigned char **data)
37
while (**data == ' ' || **data == '\t' || **data == '\n' ||
42
if (**data == '\n' || **data == '\r')
50
static void read_num(unsigned char **data, unsigned int *num)
53
while (**data >= '0' && **data <= '9') {
60
int ppm_get_data(unsigned char *data, size_t dtsz, unsigned int *width,
64
if (data[0] != 'P' || data[1] != '6')
68
skip_whitespace(&data);
69
read_num(&data, width);
70
skip_whitespace(&data);
71
read_num(&data,height);
78
* @param data Pointer to PPM data
79
* @param datasz Maximum data size
80
* @param sx Coordinate of upper left corner
81
* @param sy Coordinate of upper left corner
82
* @param maxwidth Maximum allowed width for picture
83
* @param maxheight Maximum allowed height for picture
84
* @param putpixel Putpixel function used to print bitmap
86
int ppm_draw(unsigned char *data, size_t datasz, unsigned int sx,
87
unsigned int sy, unsigned int maxwidth, unsigned int maxheight,
88
putpixel_cb_t putpixel, void *vport)
90
unsigned int width, height;
91
unsigned int maxcolor;
97
if ((data[0] != 'P') || (data[1] != '6'))
101
skip_whitespace(&data);
102
read_num(&data, &width);
103
skip_whitespace(&data);
104
read_num(&data, &height);
105
skip_whitespace(&data);
106
read_num(&data, &maxcolor);
109
if ((maxcolor == 0) || (maxcolor > 255) || (width * height > datasz))
112
coef = 255 / maxcolor;
113
if (coef * maxcolor > 255)
116
for (i = 0; i < width * height; i++) {
117
/* Crop picture if we don't fit into region */
118
if (i % width > maxwidth || i / width > maxheight) {
122
color = ((data[0] * coef) << 16) + ((data[1] * coef) << 8) +
125
(*putpixel)(vport, sx + (i % width), sy + (i / width), color);