1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
28 PGM portable graymap loader/saver.
32 a magick number value of 'P' and '2'
33 whitespace (blanks, TABs, CRs, LFs).
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
49 #include "GP_PXMCommon.h"
52 GP_RetCode
GP_LoadPGM(const char *src
, GP_Context
**res
)
54 FILE *f
= fopen(src
, "r");
61 if (fgetc(f
) != 'P' || fgetc(f
) != '2')
64 if (fscanf(f
, "%"PRIu32
"%"PRIu32
"%"PRIu32
, &w
, &h
, &gray
) < 3)
84 *res
= GP_ContextAlloc(w
, h
, type
);
88 if (GP_PXMLoad1bpp(f
, *res
))
92 if (GP_PXMLoad2bpp(f
, *res
))
96 if (GP_PXMLoad4bpp(f
, *res
))
100 if (GP_PXMLoad8bpp(f
, *res
))
114 GP_RetCode
GP_SavePGM(const char *res
, GP_Context
*src
)
119 switch (src
->pixel_type
) {
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)
147 if (GP_PXMSave1bpp(f
, src
))
151 if (GP_PXMSave2bpp(f
, src
))
156 if (GP_PXMSave8bpp(f
, src
))