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.

239 lines
8.6KB

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