libavcodec/pcx.c

Go to the documentation of this file.
00001 /*
00002  * PC Paintbrush PCX (.pcx) image decoder
00003  * Copyright (c) 2007, 2008 Ivo van Poorten
00004  *
00005  * This decoder does not support CGA palettes. I am unable to find samples
00006  * and Netpbm cannot generate them.
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 #include "avcodec.h"
00026 #include "bytestream.h"
00027 #include "bitstream.h"
00028 
00029 typedef struct PCXContext {
00030 AVFrame picture;
00031 } PCXContext;
00032 
00033 static av_cold int pcx_init(AVCodecContext *avctx) {
00034 PCXContext *s = avctx->priv_data;
00035 
00036 avcodec_get_frame_defaults(&s->picture);
00037 avctx->coded_frame= &s->picture;
00038 
00039 return 0;
00040 }
00041 
00045 static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
00046 unsigned int bytes_per_scanline) {
00047 unsigned int i = 0;
00048 unsigned char run, value;
00049 
00050 while (i<bytes_per_scanline) {
00051 run = 1;
00052 value = *src++;
00053 if (value >= 0xc0) {
00054 run = value & 0x3f;
00055 value = *src++;
00056 }
00057 while (i<bytes_per_scanline && run--)
00058 dst[i++] = value;
00059 }
00060 
00061 return src;
00062 }
00063 
00064 static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
00065 unsigned int i;
00066 
00067 for (i=0; i<pallen; i++)
00068 *dst++ = bytestream_get_be24(src);
00069 memset(dst, 0, (256 - pallen) * sizeof(*dst));
00070 }
00071 
00072 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00073 const uint8_t *buf, int buf_size) {
00074 PCXContext * const s = avctx->priv_data;
00075 AVFrame *picture = data;
00076 AVFrame * const p = &s->picture;
00077 int xmin, ymin, xmax, ymax;
00078 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
00079 bytes_per_scanline;
00080 uint8_t *ptr;
00081 uint8_t const *bufstart = buf;
00082 
00083 if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
00084 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
00085 return -1;
00086 }
00087 
00088 xmin = AV_RL16(buf+ 4);
00089 ymin = AV_RL16(buf+ 6);
00090 xmax = AV_RL16(buf+ 8);
00091 ymax = AV_RL16(buf+10);
00092 
00093 if (xmax < xmin || ymax < ymin) {
00094 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
00095 return -1;
00096 }
00097 
00098 w = xmax - xmin + 1;
00099 h = ymax - ymin + 1;
00100 
00101 bits_per_pixel = buf[3];
00102 bytes_per_line = AV_RL16(buf+66);
00103 nplanes = buf[65];
00104 bytes_per_scanline = nplanes * bytes_per_line;
00105 
00106 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
00107 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
00108 return -1;
00109 }
00110 
00111 switch ((nplanes<<8) + bits_per_pixel) {
00112 case 0x0308:
00113 avctx->pix_fmt = PIX_FMT_RGB24;
00114 break;
00115 case 0x0108:
00116 case 0x0104:
00117 case 0x0102:
00118 case 0x0101:
00119 case 0x0401:
00120 case 0x0301:
00121 case 0x0201:
00122 avctx->pix_fmt = PIX_FMT_PAL8;
00123 break;
00124 default:
00125 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
00126 return -1;
00127 }
00128 
00129 buf += 128;
00130 
00131 if (p->data[0])
00132 avctx->release_buffer(avctx, p);
00133 
00134 if (avcodec_check_dimensions(avctx, w, h))
00135 return -1;
00136 if (w != avctx->width || h != avctx->height)
00137 avcodec_set_dimensions(avctx, w, h);
00138 if (avctx->get_buffer(avctx, p) < 0) {
00139 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00140 return -1;
00141 }
00142 
00143 p->pict_type = FF_I_TYPE;
00144 
00145 ptr = p->data[0];
00146 stride = p->linesize[0];
00147 
00148 if (nplanes == 3 && bits_per_pixel == 8) {
00149 uint8_t scanline[bytes_per_scanline];
00150 
00151 for (y=0; y<h; y++) {
00152 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
00153 
00154 for (x=0; x<w; x++) {
00155 ptr[3*x ] = scanline[x ];
00156 ptr[3*x+1] = scanline[x+ bytes_per_line ];
00157 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
00158 }
00159 
00160 ptr += stride;
00161 }
00162 
00163 } else if (nplanes == 1 && bits_per_pixel == 8) {
00164 uint8_t scanline[bytes_per_scanline];
00165 const uint8_t *palstart = bufstart + buf_size - 769;
00166 
00167 for (y=0; y<h; y++, ptr+=stride) {
00168 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
00169 memcpy(ptr, scanline, w);
00170 }
00171 
00172 if (buf != palstart) {
00173 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
00174 buf = palstart;
00175 }
00176 if (*buf++ != 12) {
00177 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
00178 return -1;
00179 }
00180 
00181 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
00182 uint8_t scanline[bytes_per_scanline];
00183 GetBitContext s;
00184 
00185 for (y=0; y<h; y++) {
00186 init_get_bits(&s, scanline, bytes_per_scanline<<3);
00187 
00188 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
00189 
00190 for (x=0; x<w; x++)
00191 ptr[x] = get_bits(&s, bits_per_pixel);
00192 ptr += stride;
00193 }
00194 
00195 } else { /* planar, 4, 8 or 16 colors */
00196 uint8_t scanline[bytes_per_scanline];
00197 int i;
00198 
00199 for (y=0; y<h; y++) {
00200 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
00201 
00202 for (x=0; x<w; x++) {
00203 int m = 0x80 >> (x&7), v = 0;
00204 for (i=nplanes - 1; i>=0; i--) {
00205 v <<= 1;
00206 v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
00207 }
00208 ptr[x] = v;
00209 }
00210 ptr += stride;
00211 }
00212 }
00213 
00214 if (nplanes == 1 && bits_per_pixel == 8) {
00215 pcx_palette(&buf, (uint32_t *) p->data[1], 256);
00216 } else if (bits_per_pixel < 8) {
00217 const uint8_t *palette = bufstart+16;
00218 pcx_palette(&palette, (uint32_t *) p->data[1], 16);
00219 }
00220 
00221 *picture = s->picture;
00222 *data_size = sizeof(AVFrame);
00223 
00224 return buf - bufstart;
00225 }
00226 
00227 static av_cold int pcx_end(AVCodecContext *avctx) {
00228 PCXContext *s = avctx->priv_data;
00229 
00230 if(s->picture.data[0])
00231 avctx->release_buffer(avctx, &s->picture);
00232 
00233 return 0;
00234 }
00235 
00236 AVCodec pcx_decoder = {
00237 "pcx",
00238 CODEC_TYPE_VIDEO,
00239 CODEC_ID_PCX,
00240 sizeof(PCXContext),
00241 pcx_init,
00242 NULL,
00243 pcx_end,
00244 pcx_decode_frame,
00245 0,
00246 NULL,
00247 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
00248 };

Generated on Fri Oct 26 02:35:38 2012 for FFmpeg by doxygen 1.5.8

AltStyle によって変換されたページ (->オリジナル) /