1 /*
2  *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef INCLUDE_LIBYUV_PLANAR_FUNCTIONS_H_  // NOLINT
12 #define INCLUDE_LIBYUV_PLANAR_FUNCTIONS_H_
13 
14 #include "libyuv/basic_types.h"
15 
16 // TODO(fbarchard): Remove the following headers includes.
17 #include "libyuv/convert.h"
18 #include "libyuv/convert_argb.h"
19 
20 #ifdef __cplusplus
21 namespace libyuv {
22 extern "C" {
23 #endif
24 
25 // Copy a plane of data.
26 LIBYUV_API
27 void CopyPlane(const uint8* src_y, int src_stride_y,
28                uint8* dst_y, int dst_stride_y,
29                int width, int height);
30 
31 LIBYUV_API
32 void CopyPlane_16(const uint16* src_y, int src_stride_y,
33                   uint16* dst_y, int dst_stride_y,
34                   int width, int height);
35 
36 // Set a plane of data to a 32 bit value.
37 LIBYUV_API
38 void SetPlane(uint8* dst_y, int dst_stride_y,
39               int width, int height,
40               uint32 value);
41 
42 // Copy I400.  Supports inverting.
43 LIBYUV_API
44 int I400ToI400(const uint8* src_y, int src_stride_y,
45                uint8* dst_y, int dst_stride_y,
46                int width, int height);
47 
48 #define J400ToJ400 I400ToI400
49 
50 // Copy I422 to I422.
51 #define I422ToI422 I422Copy
52 LIBYUV_API
53 int I422Copy(const uint8* src_y, int src_stride_y,
54              const uint8* src_u, int src_stride_u,
55              const uint8* src_v, int src_stride_v,
56              uint8* dst_y, int dst_stride_y,
57              uint8* dst_u, int dst_stride_u,
58              uint8* dst_v, int dst_stride_v,
59              int width, int height);
60 
61 // Copy I444 to I444.
62 #define I444ToI444 I444Copy
63 LIBYUV_API
64 int I444Copy(const uint8* src_y, int src_stride_y,
65              const uint8* src_u, int src_stride_u,
66              const uint8* src_v, int src_stride_v,
67              uint8* dst_y, int dst_stride_y,
68              uint8* dst_u, int dst_stride_u,
69              uint8* dst_v, int dst_stride_v,
70              int width, int height);
71 
72 // Convert YUY2 to I422.
73 LIBYUV_API
74 int YUY2ToI422(const uint8* src_yuy2, int src_stride_yuy2,
75                uint8* dst_y, int dst_stride_y,
76                uint8* dst_u, int dst_stride_u,
77                uint8* dst_v, int dst_stride_v,
78                int width, int height);
79 
80 // Convert UYVY to I422.
81 LIBYUV_API
82 int UYVYToI422(const uint8* src_uyvy, int src_stride_uyvy,
83                uint8* dst_y, int dst_stride_y,
84                uint8* dst_u, int dst_stride_u,
85                uint8* dst_v, int dst_stride_v,
86                int width, int height);
87 
88 LIBYUV_API
89 int YUY2ToNV12(const uint8* src_yuy2, int src_stride_yuy2,
90                uint8* dst_y, int dst_stride_y,
91                uint8* dst_uv, int dst_stride_uv,
92                int width, int height);
93 
94 LIBYUV_API
95 int UYVYToNV12(const uint8* src_uyvy, int src_stride_uyvy,
96                uint8* dst_y, int dst_stride_y,
97                uint8* dst_uv, int dst_stride_uv,
98                int width, int height);
99 
100 // Convert I420 to I400. (calls CopyPlane ignoring u/v).
101 LIBYUV_API
102 int I420ToI400(const uint8* src_y, int src_stride_y,
103                const uint8* src_u, int src_stride_u,
104                const uint8* src_v, int src_stride_v,
105                uint8* dst_y, int dst_stride_y,
106                int width, int height);
107 
108 // Alias
109 #define J420ToJ400 I420ToI400
110 #define I420ToI420Mirror I420Mirror
111 
112 // I420 mirror.
113 LIBYUV_API
114 int I420Mirror(const uint8* src_y, int src_stride_y,
115                const uint8* src_u, int src_stride_u,
116                const uint8* src_v, int src_stride_v,
117                uint8* dst_y, int dst_stride_y,
118                uint8* dst_u, int dst_stride_u,
119                uint8* dst_v, int dst_stride_v,
120                int width, int height);
121 
122 // Alias
123 #define I400ToI400Mirror I400Mirror
124 
125 // I400 mirror.  A single plane is mirrored horizontally.
126 // Pass negative height to achieve 180 degree rotation.
127 LIBYUV_API
128 int I400Mirror(const uint8* src_y, int src_stride_y,
129                uint8* dst_y, int dst_stride_y,
130                int width, int height);
131 
132 // Alias
133 #define ARGBToARGBMirror ARGBMirror
134 
135 // ARGB mirror.
136 LIBYUV_API
137 int ARGBMirror(const uint8* src_argb, int src_stride_argb,
138                uint8* dst_argb, int dst_stride_argb,
139                int width, int height);
140 
141 // Convert NV12 to RGB565.
142 LIBYUV_API
143 int NV12ToRGB565(const uint8* src_y, int src_stride_y,
144                  const uint8* src_uv, int src_stride_uv,
145                  uint8* dst_rgb565, int dst_stride_rgb565,
146                  int width, int height);
147 
148 // I422ToARGB is in convert_argb.h
149 // Convert I422 to BGRA.
150 LIBYUV_API
151 int I422ToBGRA(const uint8* src_y, int src_stride_y,
152                const uint8* src_u, int src_stride_u,
153                const uint8* src_v, int src_stride_v,
154                uint8* dst_bgra, int dst_stride_bgra,
155                int width, int height);
156 
157 // Convert I422 to ABGR.
158 LIBYUV_API
159 int I422ToABGR(const uint8* src_y, int src_stride_y,
160                const uint8* src_u, int src_stride_u,
161                const uint8* src_v, int src_stride_v,
162                uint8* dst_abgr, int dst_stride_abgr,
163                int width, int height);
164 
165 // Convert I422 to RGBA.
166 LIBYUV_API
167 int I422ToRGBA(const uint8* src_y, int src_stride_y,
168                const uint8* src_u, int src_stride_u,
169                const uint8* src_v, int src_stride_v,
170                uint8* dst_rgba, int dst_stride_rgba,
171                int width, int height);
172 
173 // Alias
174 #define RGB24ToRAW RAWToRGB24
175 
176 LIBYUV_API
177 int RAWToRGB24(const uint8* src_raw, int src_stride_raw,
178                uint8* dst_rgb24, int dst_stride_rgb24,
179                int width, int height);
180 
181 // Draw a rectangle into I420.
182 LIBYUV_API
183 int I420Rect(uint8* dst_y, int dst_stride_y,
184              uint8* dst_u, int dst_stride_u,
185              uint8* dst_v, int dst_stride_v,
186              int x, int y, int width, int height,
187              int value_y, int value_u, int value_v);
188 
189 // Draw a rectangle into ARGB.
190 LIBYUV_API
191 int ARGBRect(uint8* dst_argb, int dst_stride_argb,
192              int x, int y, int width, int height, uint32 value);
193 
194 // Convert ARGB to gray scale ARGB.
195 LIBYUV_API
196 int ARGBGrayTo(const uint8* src_argb, int src_stride_argb,
197                uint8* dst_argb, int dst_stride_argb,
198                int width, int height);
199 
200 // Make a rectangle of ARGB gray scale.
201 LIBYUV_API
202 int ARGBGray(uint8* dst_argb, int dst_stride_argb,
203              int x, int y, int width, int height);
204 
205 // Make a rectangle of ARGB Sepia tone.
206 LIBYUV_API
207 int ARGBSepia(uint8* dst_argb, int dst_stride_argb,
208               int x, int y, int width, int height);
209 
210 // Apply a matrix rotation to each ARGB pixel.
211 // matrix_argb is 4 signed ARGB values. -128 to 127 representing -2 to 2.
212 // The first 4 coefficients apply to B, G, R, A and produce B of the output.
213 // The next 4 coefficients apply to B, G, R, A and produce G of the output.
214 // The next 4 coefficients apply to B, G, R, A and produce R of the output.
215 // The last 4 coefficients apply to B, G, R, A and produce A of the output.
216 LIBYUV_API
217 int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb,
218                     uint8* dst_argb, int dst_stride_argb,
219                     const int8* matrix_argb,
220                     int width, int height);
221 
222 // Deprecated. Use ARGBColorMatrix instead.
223 // Apply a matrix rotation to each ARGB pixel.
224 // matrix_argb is 3 signed ARGB values. -128 to 127 representing -1 to 1.
225 // The first 4 coefficients apply to B, G, R, A and produce B of the output.
226 // The next 4 coefficients apply to B, G, R, A and produce G of the output.
227 // The last 4 coefficients apply to B, G, R, A and produce R of the output.
228 LIBYUV_API
229 int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb,
230                    const int8* matrix_rgb,
231                    int x, int y, int width, int height);
232 
233 // Apply a color table each ARGB pixel.
234 // Table contains 256 ARGB values.
235 LIBYUV_API
236 int ARGBColorTable(uint8* dst_argb, int dst_stride_argb,
237                    const uint8* table_argb,
238                    int x, int y, int width, int height);
239 
240 // Apply a color table each ARGB pixel but preserve destination alpha.
241 // Table contains 256 ARGB values.
242 LIBYUV_API
243 int RGBColorTable(uint8* dst_argb, int dst_stride_argb,
244                   const uint8* table_argb,
245                   int x, int y, int width, int height);
246 
247 // Apply a luma/color table each ARGB pixel but preserve destination alpha.
248 // Table contains 32768 values indexed by [Y][C] where 7 it 7 bit luma from
249 // RGB (YJ style) and C is an 8 bit color component (R, G or B).
250 LIBYUV_API
251 int ARGBLumaColorTable(const uint8* src_argb, int src_stride_argb,
252                        uint8* dst_argb, int dst_stride_argb,
253                        const uint8* luma_rgb_table,
254                        int width, int height);
255 
256 // Apply a 3 term polynomial to ARGB values.
257 // poly points to a 4x4 matrix.  The first row is constants.  The 2nd row is
258 // coefficients for b, g, r and a.  The 3rd row is coefficients for b squared,
259 // g squared, r squared and a squared.  The 4rd row is coefficients for b to
260 // the 3, g to the 3, r to the 3 and a to the 3.  The values are summed and
261 // result clamped to 0 to 255.
262 // A polynomial approximation can be dirived using software such as 'R'.
263 
264 LIBYUV_API
265 int ARGBPolynomial(const uint8* src_argb, int src_stride_argb,
266                    uint8* dst_argb, int dst_stride_argb,
267                    const float* poly,
268                    int width, int height);
269 
270 // Quantize a rectangle of ARGB. Alpha unaffected.
271 // scale is a 16 bit fractional fixed point scaler between 0 and 65535.
272 // interval_size should be a value between 1 and 255.
273 // interval_offset should be a value between 0 and 255.
274 LIBYUV_API
275 int ARGBQuantize(uint8* dst_argb, int dst_stride_argb,
276                  int scale, int interval_size, int interval_offset,
277                  int x, int y, int width, int height);
278 
279 // Copy ARGB to ARGB.
280 LIBYUV_API
281 int ARGBCopy(const uint8* src_argb, int src_stride_argb,
282              uint8* dst_argb, int dst_stride_argb,
283              int width, int height);
284 
285 // Copy Alpha channel of ARGB to alpha of ARGB.
286 LIBYUV_API
287 int ARGBCopyAlpha(const uint8* src_argb, int src_stride_argb,
288                   uint8* dst_argb, int dst_stride_argb,
289                   int width, int height);
290 
291 // Extract the alpha channel from ARGB.
292 LIBYUV_API
293 int ARGBExtractAlpha(const uint8* src_argb, int src_stride_argb,
294                      uint8* dst_a, int dst_stride_a,
295                      int width, int height);
296 
297 // Copy Y channel to Alpha of ARGB.
298 LIBYUV_API
299 int ARGBCopyYToAlpha(const uint8* src_y, int src_stride_y,
300                      uint8* dst_argb, int dst_stride_argb,
301                      int width, int height);
302 
303 typedef void (*ARGBBlendRow)(const uint8* src_argb0, const uint8* src_argb1,
304                              uint8* dst_argb, int width);
305 
306 // Get function to Alpha Blend ARGB pixels and store to destination.
307 LIBYUV_API
308 ARGBBlendRow GetARGBBlend();
309 
310 // Alpha Blend ARGB images and store to destination.
311 // Source is pre-multiplied by alpha using ARGBAttenuate.
312 // Alpha of destination is set to 255.
313 LIBYUV_API
314 int ARGBBlend(const uint8* src_argb0, int src_stride_argb0,
315               const uint8* src_argb1, int src_stride_argb1,
316               uint8* dst_argb, int dst_stride_argb,
317               int width, int height);
318 
319 // Alpha Blend plane and store to destination.
320 // Source is not pre-multiplied by alpha.
321 LIBYUV_API
322 int BlendPlane(const uint8* src_y0, int src_stride_y0,
323                const uint8* src_y1, int src_stride_y1,
324                const uint8* alpha, int alpha_stride,
325                uint8* dst_y, int dst_stride_y,
326                int width, int height);
327 
328 // Alpha Blend YUV images and store to destination.
329 // Source is not pre-multiplied by alpha.
330 // Alpha is full width x height and subsampled to half size to apply to UV.
331 LIBYUV_API
332 int I420Blend(const uint8* src_y0, int src_stride_y0,
333               const uint8* src_u0, int src_stride_u0,
334               const uint8* src_v0, int src_stride_v0,
335               const uint8* src_y1, int src_stride_y1,
336               const uint8* src_u1, int src_stride_u1,
337               const uint8* src_v1, int src_stride_v1,
338               const uint8* alpha, int alpha_stride,
339               uint8* dst_y, int dst_stride_y,
340               uint8* dst_u, int dst_stride_u,
341               uint8* dst_v, int dst_stride_v,
342               int width, int height);
343 
344 // Multiply ARGB image by ARGB image. Shifted down by 8. Saturates to 255.
345 LIBYUV_API
346 int ARGBMultiply(const uint8* src_argb0, int src_stride_argb0,
347                  const uint8* src_argb1, int src_stride_argb1,
348                  uint8* dst_argb, int dst_stride_argb,
349                  int width, int height);
350 
351 // Add ARGB image with ARGB image. Saturates to 255.
352 LIBYUV_API
353 int ARGBAdd(const uint8* src_argb0, int src_stride_argb0,
354             const uint8* src_argb1, int src_stride_argb1,
355             uint8* dst_argb, int dst_stride_argb,
356             int width, int height);
357 
358 // Subtract ARGB image (argb1) from ARGB image (argb0). Saturates to 0.
359 LIBYUV_API
360 int ARGBSubtract(const uint8* src_argb0, int src_stride_argb0,
361                  const uint8* src_argb1, int src_stride_argb1,
362                  uint8* dst_argb, int dst_stride_argb,
363                  int width, int height);
364 
365 // Convert I422 to YUY2.
366 LIBYUV_API
367 int I422ToYUY2(const uint8* src_y, int src_stride_y,
368                const uint8* src_u, int src_stride_u,
369                const uint8* src_v, int src_stride_v,
370                uint8* dst_frame, int dst_stride_frame,
371                int width, int height);
372 
373 // Convert I422 to UYVY.
374 LIBYUV_API
375 int I422ToUYVY(const uint8* src_y, int src_stride_y,
376                const uint8* src_u, int src_stride_u,
377                const uint8* src_v, int src_stride_v,
378                uint8* dst_frame, int dst_stride_frame,
379                int width, int height);
380 
381 // Convert unattentuated ARGB to preattenuated ARGB.
382 LIBYUV_API
383 int ARGBAttenuate(const uint8* src_argb, int src_stride_argb,
384                   uint8* dst_argb, int dst_stride_argb,
385                   int width, int height);
386 
387 // Convert preattentuated ARGB to unattenuated ARGB.
388 LIBYUV_API
389 int ARGBUnattenuate(const uint8* src_argb, int src_stride_argb,
390                     uint8* dst_argb, int dst_stride_argb,
391                     int width, int height);
392 
393 // Internal function - do not call directly.
394 // Computes table of cumulative sum for image where the value is the sum
395 // of all values above and to the left of the entry. Used by ARGBBlur.
396 LIBYUV_API
397 int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb,
398                              int32* dst_cumsum, int dst_stride32_cumsum,
399                              int width, int height);
400 
401 // Blur ARGB image.
402 // dst_cumsum table of width * (height + 1) * 16 bytes aligned to
403 //   16 byte boundary.
404 // dst_stride32_cumsum is number of ints in a row (width * 4).
405 // radius is number of pixels around the center.  e.g. 1 = 3x3. 2=5x5.
406 // Blur is optimized for radius of 5 (11x11) or less.
407 LIBYUV_API
408 int ARGBBlur(const uint8* src_argb, int src_stride_argb,
409              uint8* dst_argb, int dst_stride_argb,
410              int32* dst_cumsum, int dst_stride32_cumsum,
411              int width, int height, int radius);
412 
413 // Multiply ARGB image by ARGB value.
414 LIBYUV_API
415 int ARGBShade(const uint8* src_argb, int src_stride_argb,
416               uint8* dst_argb, int dst_stride_argb,
417               int width, int height, uint32 value);
418 
419 // Interpolate between two images using specified amount of interpolation
420 // (0 to 255) and store to destination.
421 // 'interpolation' is specified as 8 bit fraction where 0 means 100% src0
422 // and 255 means 1% src0 and 99% src1.
423 LIBYUV_API
424 int InterpolatePlane(const uint8* src0, int src_stride0,
425                      const uint8* src1, int src_stride1,
426                      uint8* dst, int dst_stride,
427                      int width, int height, int interpolation);
428 
429 // Interpolate between two ARGB images using specified amount of interpolation
430 // Internally calls InterpolatePlane with width * 4 (bpp).
431 LIBYUV_API
432 int ARGBInterpolate(const uint8* src_argb0, int src_stride_argb0,
433                     const uint8* src_argb1, int src_stride_argb1,
434                     uint8* dst_argb, int dst_stride_argb,
435                     int width, int height, int interpolation);
436 
437 // Interpolate between two YUV images using specified amount of interpolation
438 // Internally calls InterpolatePlane on each plane where the U and V planes
439 // are half width and half height.
440 LIBYUV_API
441 int I420Interpolate(const uint8* src0_y, int src0_stride_y,
442                     const uint8* src0_u, int src0_stride_u,
443                     const uint8* src0_v, int src0_stride_v,
444                     const uint8* src1_y, int src1_stride_y,
445                     const uint8* src1_u, int src1_stride_u,
446                     const uint8* src1_v, int src1_stride_v,
447                     uint8* dst_y, int dst_stride_y,
448                     uint8* dst_u, int dst_stride_u,
449                     uint8* dst_v, int dst_stride_v,
450                     int width, int height, int interpolation);
451 
452 #if defined(__pnacl__) || defined(__CLR_VER) || \
453     (defined(__i386__) && !defined(__SSE__) && !defined(__clang__))
454 #define LIBYUV_DISABLE_X86
455 #endif
456 // MemorySanitizer does not support assembly code yet. http://crbug.com/344505
457 #if defined(__has_feature)
458 #if __has_feature(memory_sanitizer)
459 #define LIBYUV_DISABLE_X86
460 #endif
461 #endif
462 // The following are available on all x86 platforms:
463 #if !defined(LIBYUV_DISABLE_X86) && \
464     (defined(_M_IX86) || defined(__x86_64__) || defined(__i386__))
465 #define HAS_ARGBAFFINEROW_SSE2
466 #endif
467 
468 // Row function for copying pixels from a source with a slope to a row
469 // of destination. Useful for scaling, rotation, mirror, texture mapping.
470 LIBYUV_API
471 void ARGBAffineRow_C(const uint8* src_argb, int src_argb_stride,
472                      uint8* dst_argb, const float* uv_dudv, int width);
473 LIBYUV_API
474 void ARGBAffineRow_SSE2(const uint8* src_argb, int src_argb_stride,
475                         uint8* dst_argb, const float* uv_dudv, int width);
476 
477 // Shuffle ARGB channel order.  e.g. BGRA to ARGB.
478 // shuffler is 16 bytes and must be aligned.
479 LIBYUV_API
480 int ARGBShuffle(const uint8* src_bgra, int src_stride_bgra,
481                 uint8* dst_argb, int dst_stride_argb,
482                 const uint8* shuffler, int width, int height);
483 
484 // Sobel ARGB effect with planar output.
485 LIBYUV_API
486 int ARGBSobelToPlane(const uint8* src_argb, int src_stride_argb,
487                      uint8* dst_y, int dst_stride_y,
488                      int width, int height);
489 
490 // Sobel ARGB effect.
491 LIBYUV_API
492 int ARGBSobel(const uint8* src_argb, int src_stride_argb,
493               uint8* dst_argb, int dst_stride_argb,
494               int width, int height);
495 
496 // Sobel ARGB effect w/ Sobel X, Sobel, Sobel Y in ARGB.
497 LIBYUV_API
498 int ARGBSobelXY(const uint8* src_argb, int src_stride_argb,
499                 uint8* dst_argb, int dst_stride_argb,
500                 int width, int height);
501 
502 #ifdef __cplusplus
503 }  // extern "C"
504 }  // namespace libyuv
505 #endif
506 
507 #endif  // INCLUDE_LIBYUV_PLANAR_FUNCTIONS_H_  NOLINT
508