Added .gitignore
[gfxprim.git] / loaders / GP_PGM.c
blob77d8051327c92c64f1da0b121f647f841b73b1e4
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
28 PGM portable graymap loader/saver.
30 Format:
32 a magick number value of 'P' and '2'
33 whitespace (blanks, TABs, CRs, LFs).
34 ascii width
35 whitespace
36 ascii height
37 whitespace
38 maximal gray value (interval is 0 ... max)
39 width * height ascii gray values
41 lines starting with '#' are comments to the end of line
45 #include <stdio.h>
46 #include <stdint.h>
47 #include <inttypes.h>
49 #include "GP_PXMCommon.h"
50 #include "GP_PGM.h"
52 GP_RetCode GP_LoadPGM(const char *src, GP_Context **res)
54 FILE *f = fopen(src, "r");
55 uint32_t w, h, gray;
56 GP_PixelType type;
58 if (f == NULL)
59 return GP_EBADFILE;
61 if (fgetc(f) != 'P' || fgetc(f) != '2')
62 goto err1;
64 if (fscanf(f, "%"PRIu32"%"PRIu32"%"PRIu32, &w, &h, &gray) < 3)
65 goto err1;
67 switch (gray) {
68 case 1:
69 type = GP_PIXEL_G1;
70 break;
71 case 3:
72 type = GP_PIXEL_G2;
73 break;
74 case 15:
75 type = GP_PIXEL_G4;
76 break;
77 case 255:
78 type = GP_PIXEL_G8;
79 break;
80 default:
81 goto err1;
84 *res = GP_ContextAlloc(w, h, type);
86 switch (gray) {
87 case 1:
88 if (GP_PXMLoad1bpp(f, *res))
89 goto err2;
90 break;
91 case 3:
92 if (GP_PXMLoad2bpp(f, *res))
93 goto err2;
94 break;
95 case 15:
96 if (GP_PXMLoad4bpp(f, *res))
97 goto err2;
98 break;
99 case 255:
100 if (GP_PXMLoad8bpp(f, *res))
101 goto err2;
102 break;
105 fclose(f);
106 return GP_ESUCCESS;
107 err2:
108 free(*res);
109 err1:
110 fclose(f);
111 return GP_EBADFILE;
114 GP_RetCode GP_SavePGM(const char *res, GP_Context *src)
116 FILE *f;
117 uint32_t gray;
119 switch (src->pixel_type) {
120 case GP_PIXEL_G1:
121 gray = 1;
122 break;
123 case GP_PIXEL_G2:
124 gray = 3;
125 break;
126 case GP_PIXEL_G4:
127 gray = 15;
128 break;
129 case GP_PIXEL_G8:
130 gray = 255;
131 break;
132 default:
133 return GP_ENOIMPL;
136 f = fopen(res, "w");
138 if (f == NULL)
139 return GP_EBADFILE;
141 if (fprintf(f, "P2\n%u %u\n%u\n# Generated by gfxprim\n",
142 (unsigned int) src->w, (unsigned int) src->h, gray) < 3)
143 goto err;
145 switch (gray) {
146 case 1:
147 if (GP_PXMSave1bpp(f, src))
148 goto err;
149 break;
150 case 3:
151 if (GP_PXMSave2bpp(f, src))
152 goto err;
153 break;
154 //TODO
155 case 255:
156 if (GP_PXMSave8bpp(f, src))
157 goto err;
158 break;
159 default:
160 return GP_ENOIMPL;
163 if (fclose(f))
164 return GP_EBADFILE;
166 return GP_ESUCCESS;
167 err:
168 fclose(f);
169 return GP_EBADFILE;