Rev 2479 | Rev 3707 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2479 | Rev 2541 | ||
|---|---|---|---|
| Line 24... | Line 24... | ||
| 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; |
| Line 54... | Line 55... | ||
| 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 | ||
| Line 79... | Line 81... | ||
| 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; |
| Line 103... | Line 104... | ||
| 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 | } |