Rev 2479 | Rev 2787 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2479 | Rev 2541 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <types.h> |
29 | #include <sys/types.h> |
30 | #include <errno.h> |
30 | #include <errno.h> |
31 | 31 | ||
32 | #include "ppm.h" |
32 | #include "ppm.h" |
33 | 33 | ||
34 | static void skip_whitespace(unsigned char **data) |
34 | static void skip_whitespace(unsigned char **data) |
35 | { |
35 | { |
36 | retry: |
36 | retry: |
37 | while (**data == ' ' || **data == '\t' || **data == '\n' || **data == '\r') |
37 | while (**data == ' ' || **data == '\t' || **data == '\n' || |
- | 38 | **data == '\r') |
|
38 | (*data)++; |
39 | (*data)++; |
39 | if (**data == '#') { |
40 | if (**data == '#') { |
40 | while (1) { |
41 | while (1) { |
41 | if (**data == '\n' || **data == '\r') |
42 | if (**data == '\n' || **data == '\r') |
42 | break; |
43 | break; |
43 | (*data)++; |
44 | (*data)++; |
44 | } |
45 | } |
45 | goto retry; |
46 | goto retry; |
46 | } |
47 | } |
47 | } |
48 | } |
48 | 49 | ||
49 | static void read_num(unsigned char **data, unsigned int *num) |
50 | static void read_num(unsigned char **data, unsigned int *num) |
50 | { |
51 | { |
51 | *num = 0; |
52 | *num = 0; |
52 | while (**data >= '0' && **data <= '9') { |
53 | while (**data >= '0' && **data <= '9') { |
53 | *num *= 10; |
54 | *num *= 10; |
54 | *num += **data - '0'; |
55 | *num += **data - '0'; |
55 | (*data)++; |
56 | (*data)++; |
56 | } |
57 | } |
57 | } |
58 | } |
58 | 59 | ||
59 | int ppm_get_data(unsigned char *data, size_t dtsz, unsigned int *width, unsigned int *height) |
60 | int ppm_get_data(unsigned char *data, size_t dtsz, unsigned int *width, |
- | 61 | unsigned int *height) |
|
60 | { |
62 | { |
61 | /* Read magic */ |
63 | /* Read magic */ |
62 | if (data[0] != 'P' || data[1] != '6') |
64 | if (data[0] != 'P' || data[1] != '6') |
63 | return EINVAL; |
65 | return EINVAL; |
64 | 66 | ||
65 | data+=2; |
67 | data+=2; |
66 | skip_whitespace(&data); |
68 | skip_whitespace(&data); |
67 | read_num(&data, width); |
69 | read_num(&data, width); |
68 | skip_whitespace(&data); |
70 | skip_whitespace(&data); |
69 | read_num(&data,height); |
71 | read_num(&data,height); |
70 | 72 | ||
71 | return 0; |
73 | return 0; |
72 | } |
74 | } |
73 | 75 | ||
74 | /** Draw PPM pixmap |
76 | /** Draw PPM pixmap |
75 | * |
77 | * |
76 | * @param data Pointer to PPM data |
78 | * @param data Pointer to PPM data |
77 | * @param datasz Maximum data size |
79 | * @param datasz Maximum data size |
78 | * @param sx Coordinate of upper left corner |
80 | * @param sx Coordinate of upper left corner |
79 | * @param sy Coordinate of upper left corner |
81 | * @param sy Coordinate of upper left corner |
80 | * @param maxwidth Maximum allowed width for picture |
82 | * @param maxwidth Maximum allowed width for picture |
81 | * @param maxheight Maximum allowed height for picture |
83 | * @param maxheight Maximum allowed height for picture |
82 | * @param putpixel Putpixel function used to print bitmap |
84 | * @param putpixel Putpixel function used to print bitmap |
83 | */ |
85 | */ |
84 | int ppm_draw(unsigned char *data, size_t datasz, unsigned int sx, |
86 | int ppm_draw(unsigned char *data, size_t datasz, unsigned int sx, |
85 | unsigned int sy, |
- | |
86 | unsigned int maxwidth, unsigned int maxheight, |
87 | unsigned int sy, unsigned int maxwidth, unsigned int maxheight, |
87 | putpixel_cb_t putpixel, void *vport) |
88 | putpixel_cb_t putpixel, void *vport) |
88 | { |
89 | { |
89 | unsigned int width, height; |
90 | unsigned int width, height; |
90 | unsigned int maxcolor; |
91 | unsigned int maxcolor; |
91 | int i; |
92 | int i; |
92 | unsigned int color; |
93 | unsigned int color; |
93 | unsigned int coef; |
94 | unsigned int coef; |
94 | 95 | ||
95 | /* Read magic */ |
96 | /* Read magic */ |
96 | if (data[0] != 'P' || data[1] != '6') |
97 | if (data[0] != 'P' || data[1] != '6') |
97 | return EINVAL; |
98 | return EINVAL; |
98 | 99 | ||
99 | data+=2; |
100 | data+=2; |
100 | skip_whitespace(&data); |
101 | skip_whitespace(&data); |
101 | read_num(&data, &width); |
102 | read_num(&data, &width); |
102 | skip_whitespace(&data); |
103 | skip_whitespace(&data); |
103 | read_num(&data,&height); |
104 | read_num(&data,&height); |
104 | skip_whitespace(&data); |
105 | skip_whitespace(&data); |
105 | read_num(&data,&maxcolor); |
106 | read_num(&data,&maxcolor); |
106 | data++; |
107 | data++; |
107 | 108 | ||
108 | if (maxcolor == 0 || maxcolor > 255 || width*height > datasz) { |
109 | if (maxcolor == 0 || maxcolor > 255 || width * height > datasz) { |
109 | return EINVAL; |
110 | return EINVAL; |
110 | } |
111 | } |
111 | coef = 255/maxcolor; |
112 | coef = 255 / maxcolor; |
112 | if (coef*maxcolor > 255) |
113 | if (coef * maxcolor > 255) |
113 | coef -= 1; |
114 | coef -= 1; |
114 | 115 | ||
115 | for (i=0; i < width*height; i++) { |
116 | for (i = 0; i < width * height; i++) { |
116 | /* Crop picture if we don't fit into region */ |
117 | /* Crop picture if we don't fit into region */ |
117 | if (i % width > maxwidth || i/width > maxheight) { |
118 | if (i % width > maxwidth || i / width > maxheight) { |
118 | data += 3; |
119 | data += 3; |
119 | continue; |
120 | continue; |
120 | } |
121 | } |
121 | color = ((data[0]*coef) << 16) + ((data[1]*coef) << 8) + data[2]*coef; |
122 | color = ((data[0] * coef) << 16) + ((data[1] * coef) << 8) + |
- | 123 | data[2] * coef; |
|
122 | 124 | ||
123 | (*putpixel)(vport, sx+(i % width), sy+(i / width), color); |
125 | (*putpixel)(vport, sx + (i % width), sy + (i / width), color); |
124 | data += 3; |
126 | data += 3; |
125 | } |
127 | } |
126 | 128 | ||
127 | return 0; |
129 | return 0; |
128 | } |
130 | } |
129 | 131 |