[FFmpeg-devel] [PATCH] PCX encoder
Daniel Verkamp
daniel
Sat Mar 14 21:09:54 CET 2009
On Sat, Mar 14, 2009 at 3:50 PM, Daniel Verkamp <daniel at drv.nu> wrote:
> Hi,
>
> Attached is a patch for a PCX image encoder that handles 1-, 8-, and
> 24-bpp pixfmts. ?1 and 8 bpp tested with pbrush from Win3.11, 24 bpp
> tested with IrfanView; if anyone has a version of PC Paintbrush that
> supports 24 bpp, please test, but I am fairly sure it is correct.
>
> Thanks,
> -- Daniel Verkamp
>
Silly last-minute change broke compilation - fixed patch attached.
-------------- next part --------------
>From 3eaf674c4bd72e21b88d4c01cf5f4586d1f0a392 Mon Sep 17 00:00:00 2001
From: Daniel Verkamp <daniel at drv.nu>
Date: Sat, 14 Mar 2009 14:41:56 -0500
Subject: [PATCH] Add PCX encoder
---
Changelog | 1 +
doc/general.texi | 2 +-
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 2 +-
libavcodec/pcxenc.c | 208 ++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 212 insertions(+), 2 deletions(-)
create mode 100644 libavcodec/pcxenc.c
diff --git a/Changelog b/Changelog
index 7538ea6..0d2a16e 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ version <next>:
- deprecated vhook subsystem removed
- deprecated old scaler removed
- VQF demuxer
+- PCX encoder
diff --git a/doc/general.texi b/doc/general.texi
index 143aef4..7e84a58 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -250,7 +250,7 @@ following image formats are supported:
@tab PAM is a PNM extension with alpha support.
@item PBM @tab X @tab X
@tab Portable BitMap image
- at item PCX @tab @tab X
+ at item PCX @tab X @tab X
@tab PC Paintbrush
@item PGM @tab X @tab X
@tab Portable GrayMap image
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 13415fc..8175fc5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -159,6 +159,7 @@ OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o
OBJS-$(CONFIG_PAM_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PBM_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PCX_DECODER) += pcx.o
+OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o
OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PNG_DECODER) += png.o pngdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b024e01..bc37401 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -127,7 +127,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (NUV, nuv);
REGISTER_ENCODER (PAM, pam);
REGISTER_ENCODER (PBM, pbm);
- REGISTER_DECODER (PCX, pcx);
+ REGISTER_ENCDEC (PCX, pcx);
REGISTER_ENCODER (PGM, pgm);
REGISTER_ENCODER (PGMYUV, pgmyuv);
REGISTER_ENCDEC (PNG, png);
diff --git a/libavcodec/pcxenc.c b/libavcodec/pcxenc.c
new file mode 100644
index 0000000..e078ba8
--- /dev/null
+++ b/libavcodec/pcxenc.c
@@ -0,0 +1,208 @@
+/*
+ * PC Paintbrush PCX (.pcx) image encoder
+ * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * PCX image encoder
+ * @file libavcodec/pcxenc.c
+ * @author Daniel Verkamp
+ * @sa http://www.qzx.com/pc-gpe/pcx.txt
+ */
+
+#include "avcodec.h"
+#include "bytestream.h"
+
+typedef struct PCXContext {
+ AVFrame picture;
+} PCXContext;
+
+static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
+
+static av_cold int pcx_encode_init(AVCodecContext *avctx)
+{
+ PCXContext *s = avctx->priv_data;
+
+ avcodec_get_frame_defaults(&s->picture);
+ avctx->coded_frame = &s->picture;
+
+ return 0;
+}
+
+/**
+ * PCX run-length encoder
+ * @param dst output buffer
+ * @param dst_size size of output buffer
+ * @param src input buffer
+ * @param src_size size of input buffer
+ * @return number of bytes written to dst or -1 on error
+ */
+static int pcx_rle_encode( uint8_t *dst, int dst_size,
+ const uint8_t *src, int src_size)
+{
+ int i;
+ uint8_t prev = src[0];
+ int count = 1;
+ int dst_used = 0;
+
+ for (i = 1; i <= src_size; i++) {
+ if (i != src_size && src[i] == prev && count < 0x3F) {
+ // current byte is same as prev
+ ++count;
+ } else {
+ // output prev * count
+ if (count == 1 && prev < 0xC0) {
+ if (++dst_used > dst_size)
+ return -1;
+ *dst++ = prev;
+ } else {
+ dst_used += 2;
+ if (dst_used > dst_size)
+ return -1;
+ *dst++ = 0xC0 | count;
+ *dst++ = prev;
+ }
+
+ // get new prev
+ if (i != src_size) {
+ count = 1;
+ prev = src[i];
+ }
+ }
+ }
+
+ return dst_used;
+}
+
+static int pcx_encode_frame(AVCodecContext *avctx,
+ unsigned char *buf, int buf_size, void *data)
+{
+ PCXContext *s = avctx->priv_data;
+ AVFrame *pict = data;
+ AVFrame *const p = &s->picture;
+ uint8_t const *bufstart = buf;
+
+ int bpp, nplanes, i, j, k, l, line_bytes, filler, written, src_line_size;
+ const uint32_t *pal = NULL;
+ const uint8_t *src;
+ uint8_t *plane;
+
+ *p = *pict;
+ p->pict_type = FF_I_TYPE;
+ p->key_frame = 1;
+
+ if (avctx->width > 65535 || avctx->height > 65535) {
+ av_log(avctx, AV_LOG_ERROR, "image dimensions do not fit in 16 bits\n");
+ return -1;
+ }
+
+ switch (avctx->pix_fmt) {
+ case PIX_FMT_RGB24:
+ bpp = 8;
+ nplanes = 3;
+ break;
+ case PIX_FMT_RGB8:
+ case PIX_FMT_BGR8:
+ case PIX_FMT_RGB4_BYTE:
+ case PIX_FMT_BGR4_BYTE:
+ case PIX_FMT_GRAY8:
+ case PIX_FMT_PAL8:
+ bpp = 8;
+ nplanes = 1;
+ pal = (uint32_t *)p->data[1];
+ break;
+ case PIX_FMT_MONOBLACK:
+ bpp = 1;
+ nplanes = 1;
+ pal = monoblack_pal;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "unsupported pixfmt\n");
+ return -1;
+ }
+
+ line_bytes = (avctx->width * bpp + 7) >> 3;
+ line_bytes = (line_bytes + 1) & ~1;
+
+ bytestream_put_byte(&buf, 10); // manufacturer
+ bytestream_put_byte(&buf, 5); // version
+ bytestream_put_byte(&buf, 1); // encoding
+ bytestream_put_byte(&buf, bpp); // bits per pixel per plane
+ bytestream_put_le16(&buf, 0); // x min
+ bytestream_put_le16(&buf, 0); // y min
+ bytestream_put_le16(&buf, avctx->width - 1); // x max
+ bytestream_put_le16(&buf, avctx->height - 1); // y max
+ bytestream_put_le16(&buf, 0); // horizontal DPI
+ bytestream_put_le16(&buf, 0); // vertical DPI
+ for (i = 0; i < 16; i++)
+ bytestream_put_be24(&buf, pal ? pal[i] : 0);// palette (<= 16 color only)
+ bytestream_put_byte(&buf, 0); // reserved
+ bytestream_put_byte(&buf, nplanes); // number of planes
+ bytestream_put_le16(&buf, line_bytes); // scanline plane size in bytes
+
+ filler = 128 - (buf - bufstart);
+ memset(buf, 0, filler);
+ buf += filler;
+
+ plane = av_mallocz(line_bytes);
+ src = p->data[0];
+ src_line_size = (avctx->width * nplanes * bpp + 7) >> 3;
+
+ for (i = 0; i < avctx->height; i++) {
+ for (j = 0; j < nplanes; j++) {
+ for (k = j, l = 0; k < src_line_size; k += nplanes, l++) {
+ plane[l] = src[k];
+ }
+ if ((written = pcx_rle_encode(buf, buf_size - (buf - bufstart),
+ plane, line_bytes)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
+ return -1;
+ }
+ buf += written;
+ }
+ src += p->linesize[0];
+ }
+
+ av_free(plane);
+
+ if (nplanes == 1 && bpp == 8) {
+ bytestream_put_byte(&buf, 12);
+ for (i = 0; i < 256; i++) {
+ bytestream_put_be24(&buf, pal[i]);
+ }
+ }
+
+ return buf - bufstart;
+}
+
+AVCodec pcx_encoder = {
+ "pcx",
+ CODEC_TYPE_VIDEO,
+ CODEC_ID_PCX,
+ sizeof(PCXContext),
+ pcx_encode_init,
+ pcx_encode_frame,
+ NULL,
+ .pix_fmts = (enum PixelFormat[]){
+ PIX_FMT_RGB24,
+ PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
+ PIX_FMT_MONOBLACK,
+ PIX_FMT_NONE},
+ .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
+};
--
1.6.2
More information about the ffmpeg-devel
mailing list