Subversion Repositories HelenOS-historic

Rev

Rev 1555 | Rev 1709 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1547 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
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.
16
 *
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.
27
 */
28
 
29
#include <types.h>
30
#include <errno.h>
31
 
32
#include "ppm.h"
33
 
34
static void skip_whitespace(unsigned char **data)
35
{
36
retry:
37
    while (**data == ' ' || **data == '\t' || **data == '\n' || **data == '\r')
38
        (*data)++;
39
    if (**data == '#') {
40
        while (1) {
41
            if (**data == '\n' || **data == '\r')
42
                break;
43
            (*data)++;
44
        }
45
        goto retry;
46
    }
47
}
48
 
49
static void read_num(unsigned char **data, unsigned int *num)
50
{
51
    *num = 0;
52
    while (**data >= '0' && **data <= '9') {
53
        *num *= 10;
54
        *num += **data - '0';
55
        (*data)++;
56
    }
57
}
58
 
1555 palkovsky 59
int ppm_get_data(unsigned char *data, size_t dtsz, int *width, int *height)
60
{
61
    /* Read magic */
62
    if (data[0] != 'P' || data[1] != '6')
63
        return EINVAL;
64
 
65
    data+=2;
66
    skip_whitespace(&data);
67
    read_num(&data, width);
68
    skip_whitespace(&data);
69
    read_num(&data,height);
70
 
71
    return 0;
72
}
73
 
1547 palkovsky 74
/** Draw PPM pixmap
75
 *
76
 * @param data Pointer to PPM data
77
 * @param datasz Maximum data size
78
 * @param x Coordinate of upper left corner
79
 * @param y Coordinate of upper left corner
80
 * @param maxwidth Maximum allowed width for picture
81
 * @param maxheight Maximum allowed height for picture
82
 * @param putpixel Putpixel function used to print bitmap
83
 */
1555 palkovsky 84
int ppm_draw(unsigned char *data, size_t datasz, unsigned int sx,
1547 palkovsky 85
         unsigned int sy,
86
         unsigned int maxwidth, unsigned int maxheight,
1673 palkovsky 87
         putpixel_cb_t putpixel,void *vport)
1547 palkovsky 88
{
89
    unsigned int width, height;
90
    unsigned int maxcolor;
91
    int i;
92
    void *maxdatap = data + datasz;
93
    unsigned int color;
94
    unsigned int coef;
95
 
96
    /* Read magic */
97
    if (data[0] != 'P' || data[1] != '6')
98
        return EINVAL;
99
 
100
    data+=2;
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);
107
    data++;
108
 
109
    if (maxcolor == 0 || maxcolor > 255 || width*height > datasz) {
110
        return EINVAL;
111
    }
112
    coef = 255/maxcolor;
113
    if (coef*maxcolor > 255)
114
        coef -= 1;
115
 
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) {
119
            data += 3;
120
            continue;
121
        }
1555 palkovsky 122
        color = ((data[0]*coef) << 16) + ((data[1]*coef) << 8) + data[2]*coef;
1547 palkovsky 123
 
1673 palkovsky 124
        (*putpixel)(vport, sx+(i % width), sy+(i / width), color);
1547 palkovsky 125
        data += 3;
126
    }
127
}