1 /*
2 * BMP image format encoder
3 * Copyright (c) 2006, 2007 Michel Bardiaux
4 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
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
29
33
38 break;
41 break;
46 break;
54 break;
57 break;
58 default:
60 return -1;
61 }
62
63 return 0;
64 }
65
67 const AVFrame *pict,
int *got_packet)
68 {
69 int n_bytes_image, n_bytes_per_row, n_bytes, i,
n, hsize,
ret;
70 const uint32_t *pal = NULL;
71 uint32_t palette256[256];
72 int pad_bytes_per_row, pal_entries = 0, compression =
BMP_RGB;
76
83 pal_entries = 3;
84 break;
88 pal_entries = 3;
89 break;
97 pal = palette256;
98 break;
100 pal = (uint32_t *)p->
data[1];
101 break;
104 break;
105 }
106 if (pal && !pal_entries) pal_entries = 1 << bit_count;
107 n_bytes_per_row = ((int64_t)avctx->
width * (int64_t)bit_count + 7LL) >> 3LL;
108 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
109 n_bytes_image = avctx->
height * (n_bytes_per_row + pad_bytes_per_row);
110
111 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
112 // and related pages.
113 #define SIZE_BITMAPFILEHEADER 14
114 #define SIZE_BITMAPINFOHEADER 40
116 n_bytes = n_bytes_image + hsize;
120 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
121 bytestream_put_byte(&buf, 'M'); // do.
122 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
123 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
124 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
125 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
127 bytestream_put_le32(&buf, avctx->
width);
// BITMAPINFOHEADER.biWidth
128 bytestream_put_le32(&buf, avctx->
height);
// BITMAPINFOHEADER.biHeight
129 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
130 bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
131 bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
132 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
133 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
134 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
135 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
136 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
137 for (i = 0; i < pal_entries; i++)
138 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
139 // BMP files are bottom-to-top so we start from the end...
141 buf = pkt->
data + hsize;
142 for(i = 0; i < avctx->
height; i++) {
143 if (bit_count == 16) {
144 const uint16_t *
src = (
const uint16_t *) ptr;
145 uint16_t *dst = (uint16_t *) buf;
146 for(n = 0; n < avctx->
width; n++)
148 } else {
149 memcpy(buf, ptr, n_bytes_per_row);
150 }
151 buf += n_bytes_per_row;
152 memset(buf, 0, pad_bytes_per_row);
153 buf += pad_bytes_per_row;
154 ptr -= p->
linesize[0];
// ... and go back
155 }
156
158 *got_packet = 1;
159 return 0;
160 }
161
174 },
176 };