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.

253 lines
8.9KB

  1. /*
  2. * XWD image format
  3. *
  4. * Copyright (c) 2012 Paul B Mahol
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 = av_frame_alloc();
  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 AVPixelFormat pix_fmt = avctx->pix_fmt;
  41. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  42. uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
  43. uint32_t rgb[3] = { 0 }, bitorder = 0;
  44. uint32_t header_size;
  45. int i, out_size, ret;
  46. uint8_t *ptr, *buf;
  47. pixdepth = av_get_bits_per_pixel(desc);
  48. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  49. be = 1;
  50. switch (pix_fmt) {
  51. case AV_PIX_FMT_ARGB:
  52. case AV_PIX_FMT_BGRA:
  53. case AV_PIX_FMT_RGBA:
  54. case AV_PIX_FMT_ABGR:
  55. if (pix_fmt == AV_PIX_FMT_ARGB ||
  56. pix_fmt == AV_PIX_FMT_ABGR)
  57. be = 1;
  58. if (pix_fmt == AV_PIX_FMT_ABGR ||
  59. pix_fmt == AV_PIX_FMT_RGBA) {
  60. rgb[0] = 0xFF;
  61. rgb[1] = 0xFF00;
  62. rgb[2] = 0xFF0000;
  63. } else {
  64. rgb[0] = 0xFF0000;
  65. rgb[1] = 0xFF00;
  66. rgb[2] = 0xFF;
  67. }
  68. bpp = 32;
  69. pixdepth = 24;
  70. vclass = XWD_TRUE_COLOR;
  71. bpad = 32;
  72. break;
  73. case AV_PIX_FMT_BGR24:
  74. case AV_PIX_FMT_RGB24:
  75. if (pix_fmt == AV_PIX_FMT_RGB24)
  76. be = 1;
  77. bpp = 24;
  78. vclass = XWD_TRUE_COLOR;
  79. bpad = 32;
  80. rgb[0] = 0xFF0000;
  81. rgb[1] = 0xFF00;
  82. rgb[2] = 0xFF;
  83. break;
  84. case AV_PIX_FMT_RGB565LE:
  85. case AV_PIX_FMT_RGB565BE:
  86. case AV_PIX_FMT_BGR565LE:
  87. case AV_PIX_FMT_BGR565BE:
  88. if (pix_fmt == AV_PIX_FMT_BGR565LE ||
  89. pix_fmt == AV_PIX_FMT_BGR565BE) {
  90. rgb[0] = 0x1F;
  91. rgb[1] = 0x7E0;
  92. rgb[2] = 0xF800;
  93. } else {
  94. rgb[0] = 0xF800;
  95. rgb[1] = 0x7E0;
  96. rgb[2] = 0x1F;
  97. }
  98. bpp = 16;
  99. vclass = XWD_TRUE_COLOR;
  100. bpad = 16;
  101. break;
  102. case AV_PIX_FMT_RGB555LE:
  103. case AV_PIX_FMT_RGB555BE:
  104. case AV_PIX_FMT_BGR555LE:
  105. case AV_PIX_FMT_BGR555BE:
  106. if (pix_fmt == AV_PIX_FMT_BGR555LE ||
  107. pix_fmt == AV_PIX_FMT_BGR555BE) {
  108. rgb[0] = 0x1F;
  109. rgb[1] = 0x3E0;
  110. rgb[2] = 0x7C00;
  111. } else {
  112. rgb[0] = 0x7C00;
  113. rgb[1] = 0x3E0;
  114. rgb[2] = 0x1F;
  115. }
  116. bpp = 16;
  117. vclass = XWD_TRUE_COLOR;
  118. bpad = 16;
  119. break;
  120. case AV_PIX_FMT_RGB8:
  121. case AV_PIX_FMT_BGR8:
  122. case AV_PIX_FMT_RGB4_BYTE:
  123. case AV_PIX_FMT_BGR4_BYTE:
  124. case AV_PIX_FMT_PAL8:
  125. bpp = 8;
  126. vclass = XWD_PSEUDO_COLOR;
  127. bpad = 8;
  128. ncolors = 256;
  129. break;
  130. case AV_PIX_FMT_MONOWHITE:
  131. be = 1;
  132. bitorder = 1;
  133. bpp = 1;
  134. bpad = 8;
  135. vclass = XWD_STATIC_GRAY;
  136. break;
  137. default:
  138. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  139. return AVERROR(EINVAL);
  140. }
  141. lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
  142. header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
  143. out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
  144. if ((ret = ff_alloc_packet(pkt, out_size)) < 0) {
  145. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  146. return ret;
  147. }
  148. buf = pkt->data;
  149. avctx->coded_frame->key_frame = 1;
  150. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  151. bytestream_put_be32(&buf, header_size);
  152. bytestream_put_be32(&buf, XWD_VERSION); // file version
  153. bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
  154. bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
  155. bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
  156. bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
  157. bytestream_put_be32(&buf, 0); // bitmap x offset
  158. bytestream_put_be32(&buf, be); // byte order
  159. bytestream_put_be32(&buf, 32); // bitmap unit
  160. bytestream_put_be32(&buf, bitorder); // bit-order of image data
  161. bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
  162. bytestream_put_be32(&buf, bpp); // bits per pixel
  163. bytestream_put_be32(&buf, lsize); // bytes per scan-line
  164. bytestream_put_be32(&buf, vclass); // visual class
  165. bytestream_put_be32(&buf, rgb[0]); // red mask
  166. bytestream_put_be32(&buf, rgb[1]); // green mask
  167. bytestream_put_be32(&buf, rgb[2]); // blue mask
  168. bytestream_put_be32(&buf, 8); // size of each bitmask in bits
  169. bytestream_put_be32(&buf, ncolors); // number of colors
  170. bytestream_put_be32(&buf, ncolors); // number of entries in color map
  171. bytestream_put_be32(&buf, avctx->width); // window width
  172. bytestream_put_be32(&buf, avctx->height); // window height
  173. bytestream_put_be32(&buf, 0); // window upper left X coordinate
  174. bytestream_put_be32(&buf, 0); // window upper left Y coordinate
  175. bytestream_put_be32(&buf, 0); // window border width
  176. bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
  177. for (i = 0; i < ncolors; i++) {
  178. uint32_t val;
  179. uint8_t red, green, blue;
  180. val = AV_RN32A(p->data[1] + i * 4);
  181. red = (val >> 16) & 0xFF;
  182. green = (val >> 8) & 0xFF;
  183. blue = val & 0xFF;
  184. bytestream_put_be32(&buf, i); // colormap entry number
  185. bytestream_put_be16(&buf, red << 8);
  186. bytestream_put_be16(&buf, green << 8);
  187. bytestream_put_be16(&buf, blue << 8);
  188. bytestream_put_byte(&buf, 0x7); // bitmask flag
  189. bytestream_put_byte(&buf, 0); // padding
  190. }
  191. ptr = p->data[0];
  192. for (i = 0; i < avctx->height; i++) {
  193. bytestream_put_buffer(&buf, ptr, lsize);
  194. ptr += p->linesize[0];
  195. }
  196. pkt->flags |= AV_PKT_FLAG_KEY;
  197. *got_packet = 1;
  198. return 0;
  199. }
  200. static av_cold int xwd_encode_close(AVCodecContext *avctx)
  201. {
  202. av_freep(&avctx->coded_frame);
  203. return 0;
  204. }
  205. AVCodec ff_xwd_encoder = {
  206. .name = "xwd",
  207. .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .id = AV_CODEC_ID_XWD,
  210. .init = xwd_encode_init,
  211. .encode2 = xwd_encode_frame,
  212. .close = xwd_encode_close,
  213. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
  214. AV_PIX_FMT_RGBA,
  215. AV_PIX_FMT_ARGB,
  216. AV_PIX_FMT_ABGR,
  217. AV_PIX_FMT_RGB24,
  218. AV_PIX_FMT_BGR24,
  219. AV_PIX_FMT_RGB565BE,
  220. AV_PIX_FMT_RGB565LE,
  221. AV_PIX_FMT_BGR565BE,
  222. AV_PIX_FMT_BGR565LE,
  223. AV_PIX_FMT_RGB555BE,
  224. AV_PIX_FMT_RGB555LE,
  225. AV_PIX_FMT_BGR555BE,
  226. AV_PIX_FMT_BGR555LE,
  227. AV_PIX_FMT_RGB8,
  228. AV_PIX_FMT_BGR8,
  229. AV_PIX_FMT_RGB4_BYTE,
  230. AV_PIX_FMT_BGR4_BYTE,
  231. AV_PIX_FMT_PAL8,
  232. AV_PIX_FMT_MONOWHITE,
  233. AV_PIX_FMT_NONE },
  234. };