You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.9KB

  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. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "xwd.h"
  28. #define WINDOW_NAME "lavcxwdenc"
  29. #define WINDOW_NAME_SIZE 11
  30. static av_cold int xwd_encode_init(AVCodecContext *avctx)
  31. {
  32. avctx->coded_frame = avcodec_alloc_frame();
  33. if (!avctx->coded_frame)
  34. return AVERROR(ENOMEM);
  35. return 0;
  36. }
  37. static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  38. const AVFrame *p, int *got_packet)
  39. {
  40. enum PixelFormat pix_fmt = avctx->pix_fmt;
  41. uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
  42. uint32_t rgb[3] = { 0 }, bitorder = 0;
  43. uint32_t header_size;
  44. int i, out_size, ret;
  45. uint8_t *ptr, *buf;
  46. pixdepth = av_get_bits_per_pixel(&av_pix_fmt_descriptors[pix_fmt]);
  47. if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BE)
  48. be = 1;
  49. switch (pix_fmt) {
  50. case PIX_FMT_ARGB:
  51. case PIX_FMT_BGRA:
  52. case PIX_FMT_RGBA:
  53. case PIX_FMT_ABGR:
  54. if (pix_fmt == PIX_FMT_ARGB ||
  55. pix_fmt == PIX_FMT_ABGR)
  56. be = 1;
  57. if (pix_fmt == PIX_FMT_ABGR ||
  58. pix_fmt == PIX_FMT_RGBA) {
  59. rgb[0] = 0xFF;
  60. rgb[1] = 0xFF00;
  61. rgb[2] = 0xFF0000;
  62. } else {
  63. rgb[0] = 0xFF0000;
  64. rgb[1] = 0xFF00;
  65. rgb[2] = 0xFF;
  66. }
  67. bpp = 32;
  68. pixdepth = 24;
  69. vclass = XWD_TRUE_COLOR;
  70. bpad = 32;
  71. break;
  72. case PIX_FMT_BGR24:
  73. case PIX_FMT_RGB24:
  74. if (pix_fmt == PIX_FMT_RGB24)
  75. be = 1;
  76. bpp = 24;
  77. vclass = XWD_TRUE_COLOR;
  78. bpad = 32;
  79. rgb[0] = 0xFF0000;
  80. rgb[1] = 0xFF00;
  81. rgb[2] = 0xFF;
  82. break;
  83. case PIX_FMT_RGB565LE:
  84. case PIX_FMT_RGB565BE:
  85. case PIX_FMT_BGR565LE:
  86. case PIX_FMT_BGR565BE:
  87. if (pix_fmt == PIX_FMT_BGR565LE ||
  88. pix_fmt == PIX_FMT_BGR565BE) {
  89. rgb[0] = 0x1F;
  90. rgb[1] = 0x7E0;
  91. rgb[2] = 0xF800;
  92. } else {
  93. rgb[0] = 0xF800;
  94. rgb[1] = 0x7E0;
  95. rgb[2] = 0x1F;
  96. }
  97. bpp = 16;
  98. vclass = XWD_TRUE_COLOR;
  99. bpad = 16;
  100. break;
  101. case PIX_FMT_RGB555LE:
  102. case PIX_FMT_RGB555BE:
  103. case PIX_FMT_BGR555LE:
  104. case PIX_FMT_BGR555BE:
  105. if (pix_fmt == PIX_FMT_BGR555LE ||
  106. pix_fmt == PIX_FMT_BGR555BE) {
  107. rgb[0] = 0x1F;
  108. rgb[1] = 0x3E0;
  109. rgb[2] = 0x7C00;
  110. } else {
  111. rgb[0] = 0x7C00;
  112. rgb[1] = 0x3E0;
  113. rgb[2] = 0x1F;
  114. }
  115. bpp = 16;
  116. vclass = XWD_TRUE_COLOR;
  117. bpad = 16;
  118. break;
  119. case PIX_FMT_RGB8:
  120. case PIX_FMT_BGR8:
  121. case PIX_FMT_RGB4_BYTE:
  122. case PIX_FMT_BGR4_BYTE:
  123. case PIX_FMT_PAL8:
  124. bpp = 8;
  125. vclass = XWD_PSEUDO_COLOR;
  126. bpad = 8;
  127. ncolors = 256;
  128. break;
  129. case PIX_FMT_GRAY8:
  130. bpp = 8;
  131. bpad = 8;
  132. vclass = XWD_STATIC_GRAY;
  133. break;
  134. case PIX_FMT_MONOWHITE:
  135. be = 1;
  136. bitorder = 1;
  137. bpp = 1;
  138. bpad = 8;
  139. vclass = XWD_STATIC_GRAY;
  140. break;
  141. default:
  142. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  143. return AVERROR(EINVAL);
  144. }
  145. lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
  146. header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
  147. out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
  148. if ((ret = ff_alloc_packet2(avctx, pkt, out_size)) < 0)
  149. return ret;
  150. buf = pkt->data;
  151. avctx->coded_frame->key_frame = 1;
  152. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  153. bytestream_put_be32(&buf, header_size);
  154. bytestream_put_be32(&buf, XWD_VERSION); // file version
  155. bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
  156. bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
  157. bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
  158. bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
  159. bytestream_put_be32(&buf, 0); // bitmap x offset
  160. bytestream_put_be32(&buf, be); // byte order
  161. bytestream_put_be32(&buf, 32); // bitmap unit
  162. bytestream_put_be32(&buf, bitorder); // bit-order of image data
  163. bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
  164. bytestream_put_be32(&buf, bpp); // bits per pixel
  165. bytestream_put_be32(&buf, lsize); // bytes per scan-line
  166. bytestream_put_be32(&buf, vclass); // visual class
  167. bytestream_put_be32(&buf, rgb[0]); // red mask
  168. bytestream_put_be32(&buf, rgb[1]); // green mask
  169. bytestream_put_be32(&buf, rgb[2]); // blue mask
  170. bytestream_put_be32(&buf, 8); // size of each bitmask in bits
  171. bytestream_put_be32(&buf, ncolors); // number of colors
  172. bytestream_put_be32(&buf, ncolors); // number of entries in color map
  173. bytestream_put_be32(&buf, avctx->width); // window width
  174. bytestream_put_be32(&buf, avctx->height); // window height
  175. bytestream_put_be32(&buf, 0); // window upper left X coordinate
  176. bytestream_put_be32(&buf, 0); // window upper left Y coordinate
  177. bytestream_put_be32(&buf, 0); // window border width
  178. bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
  179. for (i = 0; i < ncolors; i++) {
  180. uint32_t val;
  181. uint8_t red, green, blue;
  182. val = AV_RN32A(p->data[1] + i * 4);
  183. red = (val >> 16) & 0xFF;
  184. green = (val >> 8) & 0xFF;
  185. blue = val & 0xFF;
  186. bytestream_put_be32(&buf, i); // colormap entry number
  187. bytestream_put_be16(&buf, red << 8);
  188. bytestream_put_be16(&buf, green << 8);
  189. bytestream_put_be16(&buf, blue << 8);
  190. bytestream_put_byte(&buf, 0x7); // bitmask flag
  191. bytestream_put_byte(&buf, 0); // padding
  192. }
  193. ptr = p->data[0];
  194. for (i = 0; i < avctx->height; i++) {
  195. bytestream_put_buffer(&buf, ptr, lsize);
  196. ptr += p->linesize[0];
  197. }
  198. pkt->flags |= AV_PKT_FLAG_KEY;
  199. *got_packet = 1;
  200. return 0;
  201. }
  202. static av_cold int xwd_encode_close(AVCodecContext *avctx)
  203. {
  204. av_freep(&avctx->coded_frame);
  205. return 0;
  206. }
  207. AVCodec ff_xwd_encoder = {
  208. .name = "xwd",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .id = AV_CODEC_ID_XWD,
  211. .init = xwd_encode_init,
  212. .encode2 = xwd_encode_frame,
  213. .close = xwd_encode_close,
  214. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_BGRA,
  215. PIX_FMT_RGBA,
  216. PIX_FMT_ARGB,
  217. PIX_FMT_ABGR,
  218. PIX_FMT_RGB24,
  219. PIX_FMT_BGR24,
  220. PIX_FMT_RGB565BE,
  221. PIX_FMT_RGB565LE,
  222. PIX_FMT_BGR565BE,
  223. PIX_FMT_BGR565LE,
  224. PIX_FMT_RGB555BE,
  225. PIX_FMT_RGB555LE,
  226. PIX_FMT_BGR555BE,
  227. PIX_FMT_BGR555LE,
  228. PIX_FMT_RGB8,
  229. PIX_FMT_BGR8,
  230. PIX_FMT_RGB4_BYTE,
  231. PIX_FMT_BGR4_BYTE,
  232. PIX_FMT_PAL8,
  233. PIX_FMT_GRAY8,
  234. PIX_FMT_MONOWHITE,
  235. PIX_FMT_NONE },
  236. .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
  237. };