pnm.c 6.2 KB
Newer Older
1 2
/*
 * PNM image format
3
 * Copyright (c) 2002, 2003 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

22 23 24
#include <stdlib.h>
#include <string.h>

25
#include "libavutil/imgutils.h"
26
#include "avcodec.h"
27
#include "pnm.h"
28

29 30
static inline int pnm_space(int c)
{
31
    return c == ' ' || c == '\n' || c == '\r' || c == '\t';
32 33
}

34
static void pnm_get(PNMContext *sc, char *str, int buf_size)
Michael Niedermayer's avatar
Michael Niedermayer committed
35
{
36 37 38 39
    char *s;
    int c;

    /* skip spaces and comments */
40
    for (;;) {
41 42
        c = *sc->bytestream++;
        if (c == '#')  {
43
            do {
44 45 46 47 48
                c = *sc->bytestream++;
            } while (c != '\n' && sc->bytestream < sc->bytestream_end);
        } else if (!pnm_space(c)) {
            break;
        }
49
    }
50

51 52 53 54 55 56 57 58
    s = str;
    while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
        if ((s - str)  < buf_size - 1)
            *s++ = c;
        c = *sc->bytestream++;
    }
    *s = '\0';
}
59

60 61
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
{
62 63 64 65
    char buf1[32], tuple_type[32];
    int h, w, depth, maxval;

    pnm_get(s, buf1, sizeof(buf1));
66 67
    s->type= buf1[1]-'0';
    if(buf1[0] != 'P')
68
        return AVERROR_INVALIDDATA;
69 70

    if (s->type==1 || s->type==4) {
71
        avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
72
    } else if (s->type==2 || s->type==5) {
73
        if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
74
            avctx->pix_fmt = AV_PIX_FMT_YUV420P;
75
        else
76
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
77
    } else if (s->type==3 || s->type==6) {
78
        avctx->pix_fmt = AV_PIX_FMT_RGB24;
79
    } else if (s->type==7) {
80 81
        w      = -1;
        h      = -1;
82
        maxval = -1;
83
        depth  = -1;
84
        tuple_type[0] = '\0';
85
        for (;;) {
86 87 88 89 90 91 92 93 94 95 96 97 98
            pnm_get(s, buf1, sizeof(buf1));
            if (!strcmp(buf1, "WIDTH")) {
                pnm_get(s, buf1, sizeof(buf1));
                w = strtol(buf1, NULL, 10);
            } else if (!strcmp(buf1, "HEIGHT")) {
                pnm_get(s, buf1, sizeof(buf1));
                h = strtol(buf1, NULL, 10);
            } else if (!strcmp(buf1, "DEPTH")) {
                pnm_get(s, buf1, sizeof(buf1));
                depth = strtol(buf1, NULL, 10);
            } else if (!strcmp(buf1, "MAXVAL")) {
                pnm_get(s, buf1, sizeof(buf1));
                maxval = strtol(buf1, NULL, 10);
99 100 101
            } else if (!strcmp(buf1, "TUPLTYPE") ||
                       /* libavcodec used to write invalid files */
                       !strcmp(buf1, "TUPLETYPE")) {
102 103 104 105
                pnm_get(s, tuple_type, sizeof(tuple_type));
            } else if (!strcmp(buf1, "ENDHDR")) {
                break;
            } else {
106
                return AVERROR_INVALIDDATA;
107 108
            }
        }
109
        /* check that all tags are present */
110
        if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
111
            return AVERROR_INVALIDDATA;
112

113
        avctx->width  = w;
114 115 116
        avctx->height = h;
        if (depth == 1) {
            if (maxval == 1)
117
                avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
118
            else
119
                avctx->pix_fmt = AV_PIX_FMT_GRAY8;
120
        } else if (depth == 3) {
121
            if (maxval < 256) {
122
            avctx->pix_fmt = AV_PIX_FMT_RGB24;
123 124
            } else {
                av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
125
                avctx->pix_fmt = AV_PIX_FMT_NONE;
126
                return AVERROR_INVALIDDATA;
127
            }
128
        } else if (depth == 4) {
129
            avctx->pix_fmt = AV_PIX_FMT_RGB32;
130
        } else {
131
            return AVERROR_INVALIDDATA;
132
        }
133 134
        return 0;
    } else {
135
        return AVERROR_INVALIDDATA;
136
    }
137 138 139
    pnm_get(s, buf1, sizeof(buf1));
    avctx->width = atoi(buf1);
    if (avctx->width <= 0)
140
        return AVERROR_INVALIDDATA;
141 142
    pnm_get(s, buf1, sizeof(buf1));
    avctx->height = atoi(buf1);
143
    if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
144
        return AVERROR_INVALIDDATA;
145
    if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
146 147
        pnm_get(s, buf1, sizeof(buf1));
        s->maxval = atoi(buf1);
148 149
        if (s->maxval <= 0) {
            av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
150
            s->maxval = 255;
151
        }
152
        if (s->maxval >= 256) {
153 154
            if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
                avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
155
                if (s->maxval != 65535)
156 157
                    avctx->pix_fmt = AV_PIX_FMT_GRAY16;
            } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
158
                if (s->maxval > 255)
159
                    avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
160 161 162 163 164 165 166
            } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
                if (s->maxval < 512)
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P9BE;
                else if (s->maxval < 1024)
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P10BE;
                else
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
167
            } else {
168
                av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
169
                avctx->pix_fmt = AV_PIX_FMT_NONE;
170
                return AVERROR_INVALIDDATA;
171
            }
172
        }
173 174
    }else
        s->maxval=1;
175
    /* more check if YUV420 */
176
    if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
177
        if ((avctx->width & 1) != 0)
178
            return AVERROR_INVALIDDATA;
179 180
        h = (avctx->height * 2);
        if ((h % 3) != 0)
181
            return AVERROR_INVALIDDATA;
182 183
        h /= 3;
        avctx->height = h;
184
    }
185
    return 0;
186
}