Revision 8975ba81

View differences:

libavformat/allformats.c
73 73
    av_register_image_format(&pbm_image_format);
74 74
    av_register_image_format(&pgm_image_format);
75 75
    av_register_image_format(&ppm_image_format);
76
    av_register_image_format(&pam_image_format);
76 77
    av_register_image_format(&pgmyuv_image_format);
77 78
    av_register_image_format(&yuv_image_format);
78 79
#ifdef CONFIG_ZLIB
libavformat/avformat.h
253 253
extern AVImageFormat pbm_image_format;
254 254
extern AVImageFormat pgm_image_format;
255 255
extern AVImageFormat ppm_image_format;
256
extern AVImageFormat pam_image_format;
256 257
extern AVImageFormat pgmyuv_image_format;
257 258
extern AVImageFormat yuv_image_format;
258 259
#ifdef CONFIG_ZLIB
libavformat/pnm.c
202 202
        put_buffer(pb, ptr, n);
203 203
        ptr += linesize;
204 204
    }
205

  
205
    
206 206
    if (info->pix_fmt == PIX_FMT_YUV420P) {
207 207
        h >>= 1;
208 208
        n >>= 1;
......
211 211
        for(i=0;i<h;i++) {
212 212
            put_buffer(pb, ptr1, n);
213 213
            put_buffer(pb, ptr2, n);
214
            ptr1 += info->pict.linesize[1];
215
            ptr2 += info->pict.linesize[2];
214
                ptr1 += info->pict.linesize[1];
215
                ptr2 += info->pict.linesize[2];
216 216
        }
217 217
    }
218 218
    put_flush_packet(pb);
219 219
    return 0;
220 220
}
221

  
222
static int pam_read(ByteIOContext *f, 
223
                    int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
224
{
225
    int i, n, linesize, h, w, depth, maxval;
226
    char buf1[32], tuple_type[32];
227
    unsigned char *ptr;
228
    AVImageInfo info1, *info = &info1;
229
    int ret;
230

  
231
    pnm_get(f, buf1, sizeof(buf1));
232
    if (strcmp(buf1, "P7") != 0)
233
        return AVERROR_INVALIDDATA;
234
    w = -1;
235
    h = -1;
236
    maxval = -1;
237
    depth = -1;
238
    tuple_type[0] = '\0';
239
    for(;;) {
240
        pnm_get(f, buf1, sizeof(buf1));
241
        if (!strcmp(buf1, "WIDTH")) {
242
            pnm_get(f, buf1, sizeof(buf1));
243
            w = strtol(buf1, NULL, 10);
244
        } else if (!strcmp(buf1, "HEIGHT")) {
245
            pnm_get(f, buf1, sizeof(buf1));
246
            h = strtol(buf1, NULL, 10);
247
        } else if (!strcmp(buf1, "DEPTH")) {
248
            pnm_get(f, buf1, sizeof(buf1));
249
            depth = strtol(buf1, NULL, 10);
250
        } else if (!strcmp(buf1, "MAXVAL")) {
251
            pnm_get(f, buf1, sizeof(buf1));
252
            maxval = strtol(buf1, NULL, 10);
253
        } else if (!strcmp(buf1, "TUPLETYPE")) {
254
            pnm_get(f, buf1, sizeof(buf1));
255
            pstrcpy(tuple_type, sizeof(tuple_type), buf1);
256
        } else if (!strcmp(buf1, "ENDHDR")) {
257
            break;
258
        } else {
259
            return AVERROR_INVALIDDATA;
260
        }
261
    }
262
    /* check that all tags are present */
263
    if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0')
264
        return AVERROR_INVALIDDATA;
265
    info->width = w;
266
    info->height = h;
267
    if (depth == 1) {
268
        if (maxval == 1)
269
            info->pix_fmt = PIX_FMT_MONOWHITE;
270
        else 
271
            info->pix_fmt = PIX_FMT_GRAY8;
272
    } else if (depth == 3) {
273
        info->pix_fmt = PIX_FMT_RGB24;
274
    } else if (depth == 4) {
275
        info->pix_fmt = PIX_FMT_RGBA32;
276
    } else {
277
        return AVERROR_INVALIDDATA;
278
    }
279
    ret = alloc_cb(opaque, info);
280
    if (ret)
281
        return ret;
221 282
    
283
    switch(info->pix_fmt) {
284
    default:
285
        return AVERROR_INVALIDDATA;
286
    case PIX_FMT_RGB24:
287
        n = info->width * 3;
288
        goto do_read;
289
    case PIX_FMT_GRAY8:
290
        n = info->width;
291
        goto do_read;
292
    case PIX_FMT_MONOWHITE:
293
        n = (info->width + 7) >> 3;
294
    do_read:
295
        ptr = info->pict.data[0];
296
        linesize = info->pict.linesize[0];
297
        for(i = 0; i < info->height; i++) {
298
            get_buffer(f, ptr, n);
299
            ptr += linesize;
300
        }
301
        break;
302
    case PIX_FMT_RGBA32:
303
        ptr = info->pict.data[0];
304
        linesize = info->pict.linesize[0];
305
        for(i = 0; i < info->height; i++) {
306
            int j, r, g, b, a;
307

  
308
            for(j = 0;j < w; j++) {
309
                r = get_byte(f);
310
                g = get_byte(f);
311
                b = get_byte(f);
312
                a = get_byte(f);
313
                ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
314
            }
315
            ptr += linesize;
316
        }
317
        break;
318
    }
319
    return 0;
320
}
321

  
322
static int pam_write(ByteIOContext *pb, AVImageInfo *info)
323
{
324
    int i, h, w, n, linesize, depth, maxval;
325
    const char *tuple_type;
326
    char buf[100];
327
    uint8_t *ptr;
328

  
329
    h = info->height;
330
    w = info->width;
331
    switch(info->pix_fmt) {
332
    case PIX_FMT_MONOWHITE:
333
        n = (info->width + 7) >> 3;
334
        depth = 1;
335
        maxval = 1;
336
        tuple_type = "BLACKANDWHITE";
337
        break;
338
    case PIX_FMT_GRAY8:
339
        n = info->width;
340
        depth = 1;
341
        maxval = 255;
342
        tuple_type = "GRAYSCALE";
343
        break;
344
    case PIX_FMT_RGB24:
345
        n = info->width * 3;
346
        depth = 3;
347
        maxval = 255;
348
        tuple_type = "RGB";
349
        break;
350
    case PIX_FMT_RGBA32:
351
        n = info->width * 4;
352
        depth = 4;
353
        maxval = 255;
354
        tuple_type = "RGB_ALPHA";
355
        break;
356
    default:
357
        return AVERROR_INVALIDDATA;
358
    }
359
    snprintf(buf, sizeof(buf), 
360
             "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
361
             w, h, depth, maxval, tuple_type);
362
    put_buffer(pb, buf, strlen(buf));
363
    
364
    ptr = info->pict.data[0];
365
    linesize = info->pict.linesize[0];
366
    
367
    if (info->pix_fmt == PIX_FMT_RGBA32) {
368
        int j;
369
        unsigned int v;
370

  
371
        for(i=0;i<h;i++) {
372
            for(j=0;j<w;j++) {
373
                v = ((uint32_t *)ptr)[j];
374
                put_byte(pb, (v >> 16) & 0xff);
375
                put_byte(pb, (v >> 8) & 0xff);
376
                put_byte(pb, (v) & 0xff);
377
                put_byte(pb, (v >> 24) & 0xff);
378
            }
379
            ptr += linesize;
380
        }
381
    } else {
382
        for(i=0;i<h;i++) {
383
            put_buffer(pb, ptr, n);
384
            ptr += linesize;
385
        }
386
    }
387
    put_flush_packet(pb);
388
    return 0;
389
}
390

  
222 391
static int pnm_probe(AVProbeData *pd)
223 392
{
224 393
    const char *p = pd->buf;
......
239 408
        return 0;
240 409
}
241 410

  
411
static int pam_probe(AVProbeData *pd)
412
{
413
    const char *p = pd->buf;
414
    if (pd->buf_size >= 8 &&
415
        p[0] == 'P' &&
416
        p[1] == '7' &&
417
        p[2] == '\n')
418
        return AVPROBE_SCORE_MAX;
419
    else
420
        return 0;
421
}
422

  
242 423
AVImageFormat pnm_image_format = {
243 424
    "pnm",
244 425
    NULL,
......
275 456
    pnm_write,
276 457
};
277 458

  
459
AVImageFormat pam_image_format = {
460
    "pam",
461
    "pam",
462
    pam_probe,
463
    pam_read,
464
    (1 << PIX_FMT_MONOWHITE) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) | 
465
    (1 << PIX_FMT_RGBA32),
466
    pam_write,
467
};
468

  
278 469
AVImageFormat pgmyuv_image_format = {
279 470
    "pgmyuv",
280 471
    NULL,

Also available in: Unified diff