summaryrefslogtreecommitdiff
path: root/glcdgraphics/pbm.c
blob: 2bca3ef41fb3e51e500b62c04dddeab99c618f1e (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * GraphLCD graphics library
 *
 * pbm.c  -  PBM file loading and saving
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2006 Andreas Regel <andreas.regel AT powarman.de>
 */

#include <stdio.h>
#include <stdint.h>
#include <syslog.h>
#include <cstdlib>

#include <cstring>

#include "bitmap.h"
#include "pbm.h"
#include "image.h"


namespace GLCD
{

cPBMFile::cPBMFile()
{
}

cPBMFile::~cPBMFile()
{
}

bool cPBMFile::Load(cImage & image, const std::string & fileName)
{
    FILE * pbmFile;
    char str[32];
    int i;
    int ch;
    int w;
    int h;

    pbmFile = fopen(fileName.c_str(), "rb");
    if (!pbmFile)
        return false;

    i = 0;
    while ((ch = getc(pbmFile)) != EOF && i < 31)
    {
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
            break;
        str[i] = ch;
        i++;
    }
    if (ch == EOF)
    {
        fclose(pbmFile);
        return false;
    }
    str[i] = 0;
    if (strcmp(str, "P4") != 0)
        return false;

    while ((ch = getc(pbmFile)) == '#')
    {
        while ((ch = getc(pbmFile)) != EOF)
        {
            if (ch == '\n' || ch == '\r')
                break;
        }
    }
    if (ch == EOF)
    {
        fclose(pbmFile);
        return false;
    }
    i = 0;
    str[i] = ch;
    i += 1;
    while ((ch = getc(pbmFile)) != EOF && i < 31)
    {
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
            break;
        str[i] = ch;
        i++;
    }
    if (ch == EOF)
    {
        fclose(pbmFile);
        return false;
    }
    str[i] = 0;
    w = atoi(str);

    i = 0;
    while ((ch = getc(pbmFile)) != EOF && i < 31)
    {
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
            break;
        str[i] = ch;
        i++;
    }
    if (ch == EOF)
    {
        fclose(pbmFile);
        return false;
    }
    str[i] = 0;
    h = atoi(str);

    image.Clear();
    image.SetWidth(w);
    image.SetHeight(h);
    image.SetDelay(100);
    unsigned char * bmpdata = new unsigned char[h * ((w + 7) / 8)];
    if (bmpdata)
    {
        if (fread(bmpdata, h * ((w + 7) / 8), 1, pbmFile) != 1)
        {
            delete[] bmpdata;
            fclose(pbmFile);
            image.Clear();
            return false;
        }
        image.AddBitmap(new cBitmap(w, h, bmpdata));
        delete[] bmpdata;
    }
    else
    {
        syslog(LOG_ERR, "glcdgraphics: malloc failed (cPBMFile::Load).");
        fclose(pbmFile);
        return false;
    }
    fclose(pbmFile);
    syslog(LOG_DEBUG, "glcdgraphics: image %s loaded.", fileName.c_str());

    return true;
}

bool cPBMFile::Save(cImage & image, const std::string & fileName)
{
    FILE * fp;
    char str[32];
    const cBitmap * bitmap;

    if (image.Count() == 1)
    {
        fp = fopen(fileName.c_str(), "wb");
        if (fp)
        {
            bitmap = image.GetBitmap(0);
            if (bitmap)
            {
                sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height());
                fwrite(str, strlen(str), 1, fp);
                fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp);
            }
            fclose(fp);
        }
    }
    else
    {
        uint16_t i;
        char tmpStr[256];

        for (i = 0; i < image.Count(); i++)
        {
            sprintf(tmpStr, "%.248s.%05d", fileName.c_str(), i);
            fp = fopen(tmpStr, "wb");
            if (fp)
            {
                bitmap = image.GetBitmap(i);
                if (bitmap)
                {
                    sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height());
                    fwrite(str, strlen(str), 1, fp);
                    fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp);
                }
                fclose(fp);
            }
        }
    }
    return true;
}

} // end of namespace