LCOV - code coverage report
Current view: top level - libavcodec - xwdenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 118 132 89.4 %
Date: 2017-12-17 04:34:43 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * XWD image format
       3             :  *
       4             :  * Copyright (c) 2012 Paul B Mahol
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "libavutil/pixdesc.h"
      25             : #include "avcodec.h"
      26             : #include "bytestream.h"
      27             : #include "internal.h"
      28             : #include "xwd.h"
      29             : 
      30             : #define WINDOW_NAME         "lavcxwdenc"
      31             : #define WINDOW_NAME_SIZE    11
      32             : 
      33         104 : static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
      34             :                             const AVFrame *pict, int *got_packet)
      35             : {
      36         104 :     enum AVPixelFormat pix_fmt = avctx->pix_fmt;
      37         104 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
      38         104 :     uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
      39         104 :     uint32_t rgb[3] = { 0 }, bitorder = 0;
      40             :     uint32_t header_size;
      41             :     int i, out_size, ret;
      42             :     uint8_t *ptr, *buf;
      43         104 :     AVFrame * const p = (AVFrame *)pict;
      44             : 
      45         104 :     pixdepth = av_get_bits_per_pixel(desc);
      46         104 :     if (desc->flags & AV_PIX_FMT_FLAG_BE)
      47          26 :         be = 1;
      48         104 :     switch (pix_fmt) {
      49          13 :     case AV_PIX_FMT_ARGB:
      50             :     case AV_PIX_FMT_BGRA:
      51             :     case AV_PIX_FMT_RGBA:
      52             :     case AV_PIX_FMT_ABGR:
      53          13 :         if (pix_fmt == AV_PIX_FMT_ARGB ||
      54             :             pix_fmt == AV_PIX_FMT_ABGR)
      55           0 :             be = 1;
      56          13 :         if (pix_fmt == AV_PIX_FMT_ABGR ||
      57             :             pix_fmt == AV_PIX_FMT_RGBA) {
      58          13 :             rgb[0] = 0xFF;
      59          13 :             rgb[1] = 0xFF00;
      60          13 :             rgb[2] = 0xFF0000;
      61             :         } else {
      62           0 :             rgb[0] = 0xFF0000;
      63           0 :             rgb[1] = 0xFF00;
      64           0 :             rgb[2] = 0xFF;
      65             :         }
      66          13 :         bpp      = 32;
      67          13 :         pixdepth = 24;
      68          13 :         vclass   = XWD_TRUE_COLOR;
      69          13 :         bpad     = 32;
      70          13 :         break;
      71          13 :     case AV_PIX_FMT_BGR24:
      72             :     case AV_PIX_FMT_RGB24:
      73          13 :         if (pix_fmt == AV_PIX_FMT_RGB24)
      74          13 :             be = 1;
      75          13 :         bpp      = 24;
      76          13 :         vclass   = XWD_TRUE_COLOR;
      77          13 :         bpad     = 32;
      78          13 :         rgb[0]   = 0xFF0000;
      79          13 :         rgb[1]   = 0xFF00;
      80          13 :         rgb[2]   = 0xFF;
      81          13 :         break;
      82          13 :     case AV_PIX_FMT_RGB565LE:
      83             :     case AV_PIX_FMT_RGB565BE:
      84             :     case AV_PIX_FMT_BGR565LE:
      85             :     case AV_PIX_FMT_BGR565BE:
      86          13 :         if (pix_fmt == AV_PIX_FMT_BGR565LE ||
      87             :             pix_fmt == AV_PIX_FMT_BGR565BE) {
      88           0 :             rgb[0] = 0x1F;
      89           0 :             rgb[1] = 0x7E0;
      90           0 :             rgb[2] = 0xF800;
      91             :         } else {
      92          13 :             rgb[0] = 0xF800;
      93          13 :             rgb[1] = 0x7E0;
      94          13 :             rgb[2] = 0x1F;
      95             :         }
      96          13 :         bpp      = 16;
      97          13 :         vclass   = XWD_TRUE_COLOR;
      98          13 :         bpad     = 16;
      99          13 :         break;
     100          13 :     case AV_PIX_FMT_RGB555LE:
     101             :     case AV_PIX_FMT_RGB555BE:
     102             :     case AV_PIX_FMT_BGR555LE:
     103             :     case AV_PIX_FMT_BGR555BE:
     104          13 :         if (pix_fmt == AV_PIX_FMT_BGR555LE ||
     105             :             pix_fmt == AV_PIX_FMT_BGR555BE) {
     106           0 :             rgb[0] = 0x1F;
     107           0 :             rgb[1] = 0x3E0;
     108           0 :             rgb[2] = 0x7C00;
     109             :         } else {
     110          13 :             rgb[0] = 0x7C00;
     111          13 :             rgb[1] = 0x3E0;
     112          13 :             rgb[2] = 0x1F;
     113             :         }
     114          13 :         bpp      = 16;
     115          13 :         vclass   = XWD_TRUE_COLOR;
     116          13 :         bpad     = 16;
     117          13 :         break;
     118          26 :     case AV_PIX_FMT_RGB8:
     119             :     case AV_PIX_FMT_BGR8:
     120             :     case AV_PIX_FMT_RGB4_BYTE:
     121             :     case AV_PIX_FMT_BGR4_BYTE:
     122             :     case AV_PIX_FMT_PAL8:
     123          26 :         bpp      = 8;
     124          26 :         vclass   = XWD_PSEUDO_COLOR;
     125          26 :         bpad     = 8;
     126          26 :         ncolors  = 256;
     127          26 :         break;
     128          13 :     case AV_PIX_FMT_GRAY8:
     129          13 :         bpp      = 8;
     130          13 :         bpad     = 8;
     131          13 :         vclass   = XWD_STATIC_GRAY;
     132          13 :         break;
     133          13 :     case AV_PIX_FMT_MONOWHITE:
     134          13 :         be       = 1;
     135          13 :         bitorder = 1;
     136          13 :         bpp      = 1;
     137          13 :         bpad     = 8;
     138          13 :         vclass   = XWD_STATIC_GRAY;
     139          13 :         break;
     140           0 :     default:
     141           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
     142           0 :         return AVERROR(EINVAL);
     143             :     }
     144             : 
     145         104 :     lsize       = FFALIGN(bpp * avctx->width, bpad) / 8;
     146         104 :     header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
     147         104 :     out_size    = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
     148             : 
     149         104 :     if ((ret = ff_alloc_packet2(avctx, pkt, out_size, 0)) < 0)
     150           0 :         return ret;
     151         104 :     buf = pkt->data;
     152             : 
     153         104 :     p->key_frame = 1;
     154         104 :     p->pict_type = AV_PICTURE_TYPE_I;
     155             : 
     156         104 :     bytestream_put_be32(&buf, header_size);
     157         104 :     bytestream_put_be32(&buf, XWD_VERSION);   // file version
     158         104 :     bytestream_put_be32(&buf, XWD_Z_PIXMAP);  // pixmap format
     159         104 :     bytestream_put_be32(&buf, pixdepth);      // pixmap depth in pixels
     160         104 :     bytestream_put_be32(&buf, avctx->width);  // pixmap width in pixels
     161         104 :     bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
     162         104 :     bytestream_put_be32(&buf, 0);             // bitmap x offset
     163         104 :     bytestream_put_be32(&buf, be);            // byte order
     164         104 :     bytestream_put_be32(&buf, 32);            // bitmap unit
     165         104 :     bytestream_put_be32(&buf, bitorder);      // bit-order of image data
     166         104 :     bytestream_put_be32(&buf, bpad);          // bitmap scan-line pad in bits
     167         104 :     bytestream_put_be32(&buf, bpp);           // bits per pixel
     168         104 :     bytestream_put_be32(&buf, lsize);         // bytes per scan-line
     169         104 :     bytestream_put_be32(&buf, vclass);        // visual class
     170         104 :     bytestream_put_be32(&buf, rgb[0]);        // red mask
     171         104 :     bytestream_put_be32(&buf, rgb[1]);        // green mask
     172         104 :     bytestream_put_be32(&buf, rgb[2]);        // blue mask
     173         104 :     bytestream_put_be32(&buf, 8);             // size of each bitmask in bits
     174         104 :     bytestream_put_be32(&buf, ncolors);       // number of colors
     175         104 :     bytestream_put_be32(&buf, ncolors);       // number of entries in color map
     176         104 :     bytestream_put_be32(&buf, avctx->width);  // window width
     177         104 :     bytestream_put_be32(&buf, avctx->height); // window height
     178         104 :     bytestream_put_be32(&buf, 0);             // window upper left X coordinate
     179         104 :     bytestream_put_be32(&buf, 0);             // window upper left Y coordinate
     180         104 :     bytestream_put_be32(&buf, 0);             // window border width
     181         104 :     bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
     182             : 
     183        6760 :     for (i = 0; i < ncolors; i++) {
     184             :         uint32_t val;
     185             :         uint8_t red, green, blue;
     186             : 
     187        6656 :         val   = AV_RN32A(p->data[1] + i * 4);
     188        6656 :         red   = (val >> 16) & 0xFF;
     189        6656 :         green = (val >>  8) & 0xFF;
     190        6656 :         blue  =  val        & 0xFF;
     191             : 
     192        6656 :         bytestream_put_be32(&buf, i);         // colormap entry number
     193        6656 :         bytestream_put_be16(&buf, red   << 8);
     194        6656 :         bytestream_put_be16(&buf, green << 8);
     195        6656 :         bytestream_put_be16(&buf, blue  << 8);
     196        6656 :         bytestream_put_byte(&buf, 0x7);       // bitmask flag
     197        6656 :         bytestream_put_byte(&buf, 0);         // padding
     198             :     }
     199             : 
     200         104 :     ptr = p->data[0];
     201       30056 :     for (i = 0; i < avctx->height; i++) {
     202       29952 :         bytestream_put_buffer(&buf, ptr, lsize);
     203       29952 :         ptr += p->linesize[0];
     204             :     }
     205             : 
     206         104 :     pkt->flags |= AV_PKT_FLAG_KEY;
     207         104 :     *got_packet = 1;
     208         104 :     return 0;
     209             : }
     210             : 
     211             : AVCodec ff_xwd_encoder = {
     212             :     .name         = "xwd",
     213             :     .long_name    = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
     214             :     .type         = AVMEDIA_TYPE_VIDEO,
     215             :     .id           = AV_CODEC_ID_XWD,
     216             :     .encode2      = xwd_encode_frame,
     217             :     .pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
     218             :                                                  AV_PIX_FMT_RGBA,
     219             :                                                  AV_PIX_FMT_ARGB,
     220             :                                                  AV_PIX_FMT_ABGR,
     221             :                                                  AV_PIX_FMT_RGB24,
     222             :                                                  AV_PIX_FMT_BGR24,
     223             :                                                  AV_PIX_FMT_RGB565BE,
     224             :                                                  AV_PIX_FMT_RGB565LE,
     225             :                                                  AV_PIX_FMT_BGR565BE,
     226             :                                                  AV_PIX_FMT_BGR565LE,
     227             :                                                  AV_PIX_FMT_RGB555BE,
     228             :                                                  AV_PIX_FMT_RGB555LE,
     229             :                                                  AV_PIX_FMT_BGR555BE,
     230             :                                                  AV_PIX_FMT_BGR555LE,
     231             :                                                  AV_PIX_FMT_RGB8,
     232             :                                                  AV_PIX_FMT_BGR8,
     233             :                                                  AV_PIX_FMT_RGB4_BYTE,
     234             :                                                  AV_PIX_FMT_BGR4_BYTE,
     235             :                                                  AV_PIX_FMT_PAL8,
     236             :                                                  AV_PIX_FMT_GRAY8,
     237             :                                                  AV_PIX_FMT_MONOWHITE,
     238             :                                                  AV_PIX_FMT_NONE },
     239             : };

Generated by: LCOV version 1.13