libavcodec/xwdenc.c
Go to the documentation of this file.
00001 /*
00002  * XWD image format
00003  *
00004  * Copyright (c) 2012 Paul B Mahol
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/pixdesc.h"
00025 #include "avcodec.h"
00026 #include "bytestream.h"
00027 #include "xwd.h"
00028 
00029 #define WINDOW_NAME         "lavcxwdenc"
00030 #define WINDOW_NAME_SIZE    11
00031 
00032 static av_cold int xwd_encode_init(AVCodecContext *avctx)
00033 {
00034     avctx->coded_frame = avcodec_alloc_frame();
00035     if (!avctx->coded_frame)
00036         return AVERROR(ENOMEM);
00037 
00038     return 0;
00039 }
00040 
00041 static int xwd_encode_frame(AVCodecContext *avctx, uint8_t *buf,
00042                             int buf_size, void *data)
00043 {
00044     AVFrame *p = data;
00045     enum PixelFormat pix_fmt = avctx->pix_fmt;
00046     uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
00047     uint32_t rgb[3] = { 0 };
00048     uint32_t header_size;
00049     int i, out_size;
00050     uint8_t *ptr;
00051 
00052     pixdepth = av_get_bits_per_pixel(&av_pix_fmt_descriptors[pix_fmt]);
00053     if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BE)
00054         be = 1;
00055     switch (pix_fmt) {
00056     case PIX_FMT_ARGB:
00057     case PIX_FMT_BGRA:
00058     case PIX_FMT_RGBA:
00059     case PIX_FMT_ABGR:
00060         if (pix_fmt == PIX_FMT_ARGB ||
00061             pix_fmt == PIX_FMT_ABGR)
00062             be = 1;
00063         if (pix_fmt == PIX_FMT_ABGR ||
00064             pix_fmt == PIX_FMT_RGBA) {
00065             rgb[0] = 0xFF;
00066             rgb[1] = 0xFF00;
00067             rgb[2] = 0xFF0000;
00068         } else {
00069             rgb[0] = 0xFF0000;
00070             rgb[1] = 0xFF00;
00071             rgb[2] = 0xFF;
00072         }
00073         bpp      = 32;
00074         pixdepth = 24;
00075         vclass   = XWD_TRUE_COLOR;
00076         bpad     = 32;
00077         break;
00078     case PIX_FMT_BGR24:
00079     case PIX_FMT_RGB24:
00080         if (pix_fmt == PIX_FMT_RGB24)
00081             be = 1;
00082         bpp      = 24;
00083         vclass   = XWD_TRUE_COLOR;
00084         bpad     = 32;
00085         rgb[0]   = 0xFF0000;
00086         rgb[1]   = 0xFF00;
00087         rgb[2]   = 0xFF;
00088         break;
00089     case PIX_FMT_RGB565LE:
00090     case PIX_FMT_RGB565BE:
00091     case PIX_FMT_BGR565LE:
00092     case PIX_FMT_BGR565BE:
00093         if (pix_fmt == PIX_FMT_BGR565LE ||
00094             pix_fmt == PIX_FMT_BGR565BE) {
00095             rgb[0] = 0x1F;
00096             rgb[1] = 0x7E0;
00097             rgb[2] = 0xF800;
00098         } else {
00099             rgb[0] = 0xF800;
00100             rgb[1] = 0x7E0;
00101             rgb[2] = 0x1F;
00102         }
00103         bpp      = 16;
00104         vclass   = XWD_TRUE_COLOR;
00105         bpad     = 16;
00106         break;
00107     case PIX_FMT_RGB555LE:
00108     case PIX_FMT_RGB555BE:
00109     case PIX_FMT_BGR555LE:
00110     case PIX_FMT_BGR555BE:
00111         if (pix_fmt == PIX_FMT_BGR555LE ||
00112             pix_fmt == PIX_FMT_BGR555BE) {
00113             rgb[0] = 0x1F;
00114             rgb[1] = 0x3E0;
00115             rgb[2] = 0x7C00;
00116         } else {
00117             rgb[0] = 0x7C00;
00118             rgb[1] = 0x3E0;
00119             rgb[2] = 0x1F;
00120         }
00121         bpp      = 16;
00122         vclass   = XWD_TRUE_COLOR;
00123         bpad     = 16;
00124         break;
00125     case PIX_FMT_RGB8:
00126     case PIX_FMT_BGR8:
00127     case PIX_FMT_RGB4_BYTE:
00128     case PIX_FMT_BGR4_BYTE:
00129     case PIX_FMT_PAL8:
00130         bpp      = 8;
00131         vclass   = XWD_PSEUDO_COLOR;
00132         bpad     = 8;
00133         ncolors  = 256;
00134         break;
00135     case PIX_FMT_MONOWHITE:
00136         bpp      = 1;
00137         bpad     = 8;
00138         vclass   = XWD_STATIC_GRAY;
00139         break;
00140     default:
00141         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00142         return AVERROR(EINVAL);
00143     }
00144 
00145     lsize       = FFALIGN(bpp * avctx->width, bpad) / 8;
00146     header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
00147     out_size    = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
00148 
00149     if (buf_size < out_size) {
00150         av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
00151         return AVERROR(ENOMEM);
00152     }
00153 
00154     avctx->coded_frame->key_frame = 1;
00155     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00156 
00157     bytestream_put_be32(&buf, header_size);
00158     bytestream_put_be32(&buf, XWD_VERSION);   // file version
00159     bytestream_put_be32(&buf, XWD_Z_PIXMAP);  // pixmap format
00160     bytestream_put_be32(&buf, pixdepth);      // pixmap depth in pixels
00161     bytestream_put_be32(&buf, avctx->width);  // pixmap width in pixels
00162     bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
00163     bytestream_put_be32(&buf, 0);             // bitmap x offset
00164     bytestream_put_be32(&buf, be);            // byte order
00165     bytestream_put_be32(&buf, 32);            // bitmap unit
00166     bytestream_put_be32(&buf, be);            // bit-order of image data
00167     bytestream_put_be32(&buf, bpad);          // bitmap scan-line pad in bits
00168     bytestream_put_be32(&buf, bpp);           // bits per pixel
00169     bytestream_put_be32(&buf, lsize);         // bytes per scan-line
00170     bytestream_put_be32(&buf, vclass);        // visual class
00171     bytestream_put_be32(&buf, rgb[0]);        // red mask
00172     bytestream_put_be32(&buf, rgb[1]);        // green mask
00173     bytestream_put_be32(&buf, rgb[2]);        // blue mask
00174     bytestream_put_be32(&buf, 8);             // size of each bitmask in bits
00175     bytestream_put_be32(&buf, ncolors);       // number of colors
00176     bytestream_put_be32(&buf, ncolors);       // number of entries in color map
00177     bytestream_put_be32(&buf, avctx->width);  // window width
00178     bytestream_put_be32(&buf, avctx->height); // window height
00179     bytestream_put_be32(&buf, 0);             // window upper left X coordinate
00180     bytestream_put_be32(&buf, 0);             // window upper left Y coordinate
00181     bytestream_put_be32(&buf, 0);             // window border width
00182     bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
00183 
00184     for (i = 0; i < ncolors; i++) {
00185         uint32_t val;
00186         uint8_t red, green, blue;
00187 
00188         val   = AV_RN32A(p->data[1] + i * 4);
00189         red   = (val >> 16) & 0xFF;
00190         green = (val >>  8) & 0xFF;
00191         blue  =  val        & 0xFF;
00192 
00193         bytestream_put_be32(&buf, i);         // colormap entry number
00194         bytestream_put_be16(&buf, red   << 8);
00195         bytestream_put_be16(&buf, green << 8);
00196         bytestream_put_be16(&buf, blue  << 8);
00197         bytestream_put_byte(&buf, 0x7);       // bitmask flag
00198         bytestream_put_byte(&buf, 0);         // padding
00199     }
00200 
00201     ptr = p->data[0];
00202     for (i = 0; i < avctx->height; i++) {
00203         bytestream_put_buffer(&buf, ptr, lsize);
00204         ptr += p->linesize[0];
00205     }
00206 
00207     return out_size;
00208 }
00209 
00210 static av_cold int xwd_encode_close(AVCodecContext *avctx)
00211 {
00212     av_freep(&avctx->coded_frame);
00213 
00214     return 0;
00215 }
00216 
00217 AVCodec ff_xwd_encoder = {
00218     .name         = "xwd",
00219     .type         = AVMEDIA_TYPE_VIDEO,
00220     .id           = CODEC_ID_XWD,
00221     .init         = xwd_encode_init,
00222     .encode       = xwd_encode_frame,
00223     .close        = xwd_encode_close,
00224     .pix_fmts     = (const enum PixelFormat[]) { PIX_FMT_BGRA,
00225                                                  PIX_FMT_RGBA,
00226                                                  PIX_FMT_ARGB,
00227                                                  PIX_FMT_ABGR,
00228                                                  PIX_FMT_RGB24,
00229                                                  PIX_FMT_BGR24,
00230                                                  PIX_FMT_RGB565BE,
00231                                                  PIX_FMT_RGB565LE,
00232                                                  PIX_FMT_BGR565BE,
00233                                                  PIX_FMT_BGR565LE,
00234                                                  PIX_FMT_RGB555BE,
00235                                                  PIX_FMT_RGB555LE,
00236                                                  PIX_FMT_BGR555BE,
00237                                                  PIX_FMT_BGR555LE,
00238                                                  PIX_FMT_RGB8,
00239                                                  PIX_FMT_BGR8,
00240                                                  PIX_FMT_RGB4_BYTE,
00241                                                  PIX_FMT_BGR4_BYTE,
00242                                                  PIX_FMT_PAL8,
00243                                                  PIX_FMT_MONOWHITE,
00244                                                  PIX_FMT_NONE },
00245     .long_name    = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
00246 };