1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * misc image utilities
22 */
23
31
34 {
35 int i;
36 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
37 if (max_pixstep_comps)
38 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
39
40 for (i = 0; i < 4; i++) {
44 if (max_pixstep_comps)
45 max_pixstep_comps[comp->
plane] = i;
46 }
47 }
48 }
49
50 static inline
52 int max_step, int max_step_comp,
54 {
55 int s, shifted_w, linesize;
56
57 if (!desc)
59
60 if (width < 0)
62 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->
log2_chroma_w : 0;
63 shifted_w = ((width + (1 << s) - 1)) >> s;
64 if (shifted_w && max_step > INT_MAX / shifted_w)
66 linesize = max_step * shifted_w;
67
69 linesize = (linesize + 7) >> 3;
70 return linesize;
71 }
72
74 {
76 int max_step [4]; /* max pixel step for each plane */
77 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
78
81
84 }
85
87 {
90 int max_step [4]; /* max pixel step for each plane */
91 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
92
93 memset(linesizes, 0, 4*sizeof(linesizes[0]));
94
97
99 for (i = 0; i < 4; i++) {
103 }
104
105 return 0;
106 }
107
109 uint8_t *ptr,
const int linesizes[4])
110 {
111 int i, total_size,
size[4] = { 0 }, has_plane[4] = { 0 };
112
114 memset(data , 0, sizeof(data[0])*4);
115
118
119 data[0] = ptr;
120 if (linesizes[0] > (INT_MAX - 1024) / height)
122 size[0] = linesizes[0] *
height;
123
126 size[0] = (size[0] + 3) & ~3;
127 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
128 return size[0] + 256 * 4;
129 }
130
131 for (i = 0; i < 4; i++)
133
134 total_size = size[0];
135 for (i = 1; i < 4 && has_plane[i]; i++) {
137 data[i] = data[i-1] + size[i-1];
138 h = (height + (1 << s) - 1) >> s;
139 if (linesizes[i] > INT_MAX / h)
141 size[i] = h * linesizes[i];
142 if (total_size > INT_MAX - size[i])
144 total_size += size[i];
145 }
146
147 return total_size;
148 }
149
151 {
152 int i;
153
154 for (i = 0; i < 256; i++) {
156
157 switch (pix_fmt) {
159 r = (i>>5 )*36;
160 g = ((i>>2)&7)*36;
161 b = (i&3 )*85;
162 break;
164 b = (i>>6 )*85;
165 g = ((i>>3)&7)*36;
166 r = (i&7 )*36;
167 break;
169 r = (i>>3 )*255;
170 g = ((i>>1)&3)*85;
171 b = (i&1 )*255;
172 break;
174 b = (i>>3 )*255;
175 g = ((i>>1)&3)*85;
176 r = (i&1 )*255;
177 break;
179 r = b = g = i;
180 break;
181 default:
183 }
184 pal[i] = b + (g<<8) + (r<<16) + (0xFF
U<<24);
185 }
186
187 return 0;
188 }
189
192 {
196
197 if (!desc)
199
204
205 for (i = 0; i < 4; i++)
206 linesizes[i] =
FFALIGN(linesizes[i], align);
207
211 if (!buf)
216 }
219
221 }
222
228
230
232 {
234
235 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
236 return 0;
237
240 }
241
244 int bytewidth,
int height)
245 {
246 if (!dst || !src)
247 return;
250 for (;height > 0; height--) {
251 memcpy(dst, src, bytewidth);
252 dst += dst_linesize;
253 src += src_linesize;
254 }
255 }
256
258 const uint8_t *src_data[4],
const int src_linesizes[4],
260 {
262
264 return;
265
269 src_data[0], src_linesizes[0],
270 width, height);
271 /* copy the palette */
272 memcpy(dst_data[1], src_data[1], 4*256);
273 } else {
274 int i, planes_nb = 0;
275
278
279 for (i = 0; i < planes_nb; i++) {
282 if (bwidth < 0) {
284 return;
285 }
286 if (i == 1 || i == 2) {
288 }
290 src_data[i], src_linesizes[i],
291 bwidth, h);
292 }
293 }
294 }
295
299 {
301
304
307
308 for (i = 0; i < 4; i++)
309 dst_linesize[i] =
FFALIGN(dst_linesize[i], align);
310
313
315 }
316
318 {
321 int linesize[4];
322
323 if (!desc)
328 // do not include palette for these pseudo-paletted formats
331 }
332
334 const uint8_t *
const src_data[4],
const int src_linesize[4],
336 {
337 int i, j, nb_planes = 0, linesize[4];
340
341 if (size > dst_size || size < 0)
343
346 nb_planes++;
347
349 for (i = 0; i < nb_planes; i++) {
352 h = (height + (1 << shift) - 1) >> shift;
353
354 for (j = 0; j < h; j++) {
355 memcpy(dst,
src, linesize[i]);
356 dst +=
FFALIGN(linesize[i], align);
357 src += src_linesize[i];
358 }
359 }
360
362 uint32_t *
d32 = (uint32_t *)(((
size_t)dst + 3) & ~3);
363 for (i = 0; i<256; i++)
365 }
366
368 }