2 * PNM image files loader
4 * copyleft (C) 2005-2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * You can alternatively redistribute this file and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License as published by the Free Software Foundation; either
25 * version 2.1 of the License, or (at your option) any later version.
30 * \brief PNM image files loader
37 #include "pnm_loader.h"
40 * \brief skips whitespace and comments
41 * \param f file to read from
43 static void ppm_skip(FILE *f
) {
51 } while (c
!= EOF
&& (isspace(c
) || comment
));
56 #define MAXDIM (16 * 1024)
58 uint8_t *read_pnm(FILE *f
, int *width
, int *height
,
59 int *bytes_per_pixel
, int *maxval
) {
62 unsigned w
, h
, m
, val
, bpp
;
63 *width
= *height
= *bytes_per_pixel
= *maxval
= 0;
68 if (type
!= '5' && type
!= '6')
71 if (fscanf(f
, "%u", &w
) != 1)
74 if (fscanf(f
, "%u", &h
) != 1)
77 if (fscanf(f
, "%u", &m
) != 1)
82 if (w
> MAXDIM
|| h
> MAXDIM
)
84 bpp
= (m
> 255) ? 2 : 1;
87 data
= malloc(w
* h
* bpp
);
88 if (fread(data
, w
* bpp
, h
, f
) != h
) {
94 *bytes_per_pixel
= bpp
;