sgi.c 10.7 KB
Newer Older
1 2 3 4
/*
 * SGI image format
 * Todd Kirby <doubleshot@pacbell.net>
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 */

#include "avformat.h"
#include "avio.h"

/* #define DEBUG */

/* sgi image file signature */
#define SGI_MAGIC 474

#define SGI_HEADER_SIZE 512

#define SGI_GRAYSCALE 1
#define SGI_RGB 3
#define SGI_RGBA 4

#define SGI_SINGLE_CHAN 2
#define SGI_MULTI_CHAN 3

typedef struct SGIInfo{
    short magic;
    char rle;
    char bytes_per_channel;
    unsigned short dimension;
    unsigned short xsize;
    unsigned short ysize;
    unsigned short zsize;
} SGIInfo;


static int sgi_probe(AVProbeData *pd)
{
    /* test for sgi magic */
53
    if (pd->buf_size >= 2 && AV_RB16(&pd->buf[0]) == SGI_MAGIC) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        return AVPROBE_SCORE_MAX;
    } else {
        return 0;
    }
}

/* read sgi header fields */
static void read_sgi_header(ByteIOContext *f, SGIInfo *info)
{
    info->magic = (unsigned short) get_be16(f);
    info->rle = get_byte(f);
    info->bytes_per_channel = get_byte(f);
    info->dimension = (unsigned short)get_be16(f);
    info->xsize = (unsigned short) get_be16(f);
    info->ysize = (unsigned short) get_be16(f);
    info->zsize = (unsigned short) get_be16(f);
70 71

    if(info->zsize > 4096)
72
        info->zsize= 0;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

#ifdef DEBUG
    printf("sgi header fields:\n");
    printf("  magic: %d\n", info->magic);
    printf("    rle: %d\n", info->rle);
    printf("    bpc: %d\n", info->bytes_per_channel);
    printf("    dim: %d\n", info->dimension);
    printf("  xsize: %d\n", info->xsize);
    printf("  ysize: %d\n", info->ysize);
    printf("  zsize: %d\n", info->zsize);
#endif

    return;
}


/* read an uncompressed sgi image */
90
static int read_uncompressed_sgi(const SGIInfo *si,
91 92 93
        AVPicture *pict, ByteIOContext *f)
{
    int x, y, z, chan_offset, ret = 0;
94
    uint8_t *dest_row;
95

96
    /* skip header */
97 98 99 100 101 102 103 104
    url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);

    pict->linesize[0] = si->xsize;

    for (z = 0; z < si->zsize; z++) {

#ifndef WORDS_BIGENDIAN
        /* rgba -> bgra for rgba32 on little endian cpus */
105
        if (si->zsize == 4 && z != 3)
106 107 108 109
            chan_offset = 2 - z;
        else
#endif
            chan_offset = z;
110

111 112 113 114
        for (y = si->ysize - 1; y >= 0; y--) {
            dest_row = pict->data[0] + (y * si->xsize * si->zsize);

            for (x = 0; x < si->xsize; x++) {
115
                dest_row[chan_offset] = get_byte(f);
116 117 118 119 120 121 122 123 124 125
                dest_row += si->zsize;
            }
        }
    }

    return ret;
}


/* expand an rle row into a channel */
126
static int expand_rle_row(ByteIOContext *f, unsigned char *optr,
127 128 129
        int chan_offset, int pixelstride)
{
    unsigned char pixel, count;
130
    int length = 0;
131

132 133 134 135 136 137
#ifndef WORDS_BIGENDIAN
    /* rgba -> bgra for rgba32 on little endian cpus */
    if (pixelstride == 4 && chan_offset != 3) {
       chan_offset = 2 - chan_offset;
    }
#endif
138

139 140 141
    optr += chan_offset;

    while (1) {
142
        pixel = get_byte(f);
143 144

        if (!(count = (pixel & 0x7f))) {
145
            return length;
146 147 148
        }
        if (pixel & 0x80) {
            while (count--) {
149 150
                *optr = get_byte(f);
                length++;
151 152 153
                optr += pixelstride;
            }
        } else {
154
            pixel = get_byte(f);
155 156 157

            while (count--) {
                *optr = pixel;
158
                length++;
159 160 161 162 163 164 165 166
                optr += pixelstride;
            }
        }
    }
}


/* read a run length encoded sgi image */
167
static int read_rle_sgi(const SGIInfo *sgi_info,
168 169
        AVPicture *pict, ByteIOContext *f)
{
170 171
    uint8_t *dest_row;
    unsigned long *start_table;
172
    int y, z, xsize, ysize, zsize, tablen;
173
    long start_offset;
174 175 176 177 178 179
    int ret = 0;

    xsize = sgi_info->xsize;
    ysize = sgi_info->ysize;
    zsize = sgi_info->zsize;

180
    /* skip header */
181 182 183 184 185 186 187 188
    url_fseek(f, SGI_HEADER_SIZE, SEEK_SET);

    /* size of rle offset and length tables */
    tablen = ysize * zsize * sizeof(long);

    start_table = (unsigned long *)av_malloc(tablen);

    if (!get_buffer(f, (uint8_t *)start_table, tablen)) {
189
        ret = AVERROR_IO;
190 191 192
        goto fail;
    }

193
    /* skip run length table */
194
    url_fseek(f, tablen, SEEK_CUR);
195 196 197 198 199

    for (z = 0; z < zsize; z++) {
        for (y = 0; y < ysize; y++) {
            dest_row = pict->data[0] + (ysize - 1 - y) * (xsize * zsize);

200
            start_offset = AV_RB32(&start_table[y + z * ysize]);
201

202
            /* don't seek if already at the next rle start offset */
203 204 205 206
            if (url_ftell(f) != start_offset) {
                url_fseek(f, start_offset, SEEK_SET);
            }

207 208 209 210
            if (expand_rle_row(f, dest_row, z, zsize) != xsize) {
              ret =  AVERROR_INVALIDDATA;
              goto fail;
            }
211 212 213 214 215 216 217 218 219 220
        }
    }

fail:
    av_free(start_table);

    return ret;
}


221
static int sgi_read(ByteIOContext *f,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
{
    SGIInfo sgi_info, *s = &sgi_info;
    AVImageInfo info1, *info = &info1;
    int ret;

    read_sgi_header(f, s);

    if (s->bytes_per_channel != 1) {
        return AVERROR_INVALIDDATA;
    }

    /* check for supported image dimensions */
    if (s->dimension != 2 && s->dimension != 3) {
        return AVERROR_INVALIDDATA;
    }

    if (s->zsize == SGI_GRAYSCALE) {
        info->pix_fmt = PIX_FMT_GRAY8;
    } else if (s->zsize == SGI_RGB) {
        info->pix_fmt = PIX_FMT_RGB24;
    } else if (s->zsize == SGI_RGBA) {
        info->pix_fmt = PIX_FMT_RGBA32;
    } else {
        return AVERROR_INVALIDDATA;
    }

    info->width = s->xsize;
    info->height = s->ysize;

    ret = alloc_cb(opaque, info);
    if (ret)
        return ret;

    if (s->rle) {
        return read_rle_sgi(s, &info->pict, f);
    } else {
        return read_uncompressed_sgi(s, &info->pict, f);
    }

    return 0; /* not reached */
}

265
#ifdef CONFIG_MUXERS
266 267 268 269 270 271
static void write_sgi_header(ByteIOContext *f, const SGIInfo *info)
{
    int i;

    put_be16(f, SGI_MAGIC);
    put_byte(f, info->rle);
272
    put_byte(f, info->bytes_per_channel);
273 274 275 276 277 278
    put_be16(f, info->dimension);
    put_be16(f, info->xsize);
    put_be16(f, info->ysize);
    put_be16(f, info->zsize);

    /* The rest are constant in this implementation */
279 280 281
    put_be32(f, 0L); /* pixmin */
    put_be32(f, 255L); /* pixmax */
    put_be32(f, 0L); /* dummy */
282 283 284 285 286 287

    /* name */
    for (i = 0; i < 80; i++) {
        put_byte(f, 0);
    }

288
    put_be32(f, 0L); /* colormap */
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

    /* The rest of the 512 byte header is unused. */
    for (i = 0; i < 404; i++) {
        put_byte(f, 0);
    }
}


static int rle_row(ByteIOContext *f, char *row, int stride, int rowsize)
{
    int length, count, i, x;
    char *start, repeat = 0;

    for (x = rowsize, length = 0; x > 0;) {
        start = row;
        row += (2 * stride);
        x -= 2;

307
        while (x > 0 && (row[-2 * stride] != row[-1 * stride] ||
308 309 310 311 312 313 314 315 316 317 318 319 320
                    row[-1 * stride] != row[0])) {
            row += stride;
            x--;
        };

        row -= (2 * stride);
        x += 2;

        count = (row - start) / stride;
        while (count > 0) {
            i = count > 126 ? 126 : count;
            count -= i;

321
            put_byte(f, 0x80 | i);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
            length++;

            while (i > 0) {
                put_byte(f, *start);
                start += stride;
                i--;
                length++;
            };
        };

        if (x <= 0) {
            break;
        }

        start = row;
        repeat = row[0];

        row += stride;
        x--;

        while (x > 0 && *row == repeat) {
            row += stride;
            x--;
        };

        count = (row - start) / stride;
        while (count > 0) {
            i = count > 126 ? 126 : count;
            count -= i;

            put_byte(f, i);
            length++;

355
            put_byte(f, repeat);
356 357 358 359 360 361
            length++;
        };
    };

    length++;

362
    put_byte(f, 0);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    return (length);
}


static int sgi_write(ByteIOContext *pb, AVImageInfo *info)
{
    SGIInfo sgi_info, *si = &sgi_info;
    long *offsettab, *lengthtab;
    int i, y, z;
    int tablesize, chan_offset;
    uint8_t *srcrow;

    si->xsize = info->width;
    si->ysize = info->height;
    si->rle = 1;
    si->bytes_per_channel = 1;
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    switch(info->pix_fmt) {
        case PIX_FMT_GRAY8:
            si->dimension = SGI_SINGLE_CHAN;
            si->zsize = SGI_GRAYSCALE;
            break;
        case PIX_FMT_RGB24:
            si->dimension = SGI_MULTI_CHAN;
            si->zsize = SGI_RGB;
            break;
         case PIX_FMT_RGBA32:
            si->dimension = SGI_MULTI_CHAN;
            si->zsize = SGI_RGBA;
            break;
        default:
            return AVERROR_INVALIDDATA;
    }

397
    write_sgi_header(pb, si);
398 399

    tablesize = si->zsize * si->ysize * sizeof(long);
400

401 402 403
    /* skip rle offset and length tables, write them at the end. */
    url_fseek(pb, tablesize * 2, SEEK_CUR);
    put_flush_packet(pb);
404

405 406 407 408 409 410 411
    lengthtab = av_malloc(tablesize);
    offsettab = av_malloc(tablesize);

    for (z = 0; z < si->zsize; z++) {

#ifndef WORDS_BIGENDIAN
        /* rgba -> bgra for rgba32 on little endian cpus */
412
        if (si->zsize == 4 && z != 3)
413 414 415 416
            chan_offset = 2 - z;
        else
#endif
            chan_offset = z;
417

418
        srcrow = info->pict.data[0] + chan_offset;
419

420 421 422 423
        for (y = si->ysize -1; y >= 0; y--) {
            offsettab[(z * si->ysize) + y] = url_ftell(pb);
            lengthtab[(z * si->ysize) + y] = rle_row(pb, srcrow,
                    si->zsize, si->xsize);
424
            srcrow += info->pict.linesize[0];
425 426 427 428
        }
    }

    url_fseek(pb, 512, SEEK_SET);
429

430 431 432 433
    /* write offset table */
    for (i = 0; i < (si->ysize * si->zsize); i++) {
        put_be32(pb, offsettab[i]);
    }
434

435 436 437 438 439 440
    /* write length table */
    for (i = 0; i < (si->ysize * si->zsize); i++) {
        put_be32(pb, lengthtab[i]);
    }

    put_flush_packet(pb);
441

442 443 444 445 446
    av_free(lengthtab);
    av_free(offsettab);

    return 0;
}
447
#endif // CONFIG_MUXERS
448 449 450 451 452 453

AVImageFormat sgi_image_format = {
    "sgi",
    "sgi,rgb,rgba,bw",
    sgi_probe,
    sgi_read,
454
    (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_RGBA32),
455
#ifdef CONFIG_MUXERS
456 457 458
    sgi_write,
#else
    NULL,
459
#endif // CONFIG_MUXERS
460
};