2 /* palette_neon_intrinsics.c - NEON optimised palette expansion functions
4 * Copyright (c) 2018 Cosmin Truta
5 * Copyright (c) 2017-2018 Arm Holdings. All rights reserved.
6 * Written by Richard Townsend <Richard.Townsend@arm.com>, February 2017.
8 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h
13 #include "../pngpriv.h"
15 #if PNG_ARM_NEON_IMPLEMENTATION == 1
17 #if defined(_MSC_VER) && defined(_M_ARM64)
18 # include <arm64_neon.h>
20 # include <arm_neon.h>
23 /* Build an RGBA palette from the RGB and separate alpha palettes. */
25 png_riffle_palette_rgba(png_structrp png_ptr
, png_row_infop row_info
)
27 png_const_colorp palette
= png_ptr
->palette
;
28 png_bytep riffled_palette
= png_ptr
->riffled_palette
;
29 png_const_bytep trans_alpha
= png_ptr
->trans_alpha
;
30 int num_trans
= png_ptr
->num_trans
;
33 /* Initially black, opaque. */
41 if (row_info
->bit_depth
!= 8)
43 png_error(png_ptr
, "bit_depth must be 8 for png_riffle_palette_rgba");
47 /* First, riffle the RGB colours into a RGBA palette, the A value is
48 * set to opaque for now.
50 for (i
= 0; i
< (1 << row_info
->bit_depth
); i
+= 16)
52 uint8x16x3_t v
= vld3q_u8((png_const_bytep
)(palette
+ i
));
56 vst4q_u8(riffled_palette
+ (i
<< 2), w
);
59 /* Fix up the missing transparency values. */
60 for (i
= 0; i
< num_trans
; i
++)
61 riffled_palette
[(i
<< 2) + 3] = trans_alpha
[i
];
64 /* Expands a palettized row into RGBA. */
66 png_do_expand_palette_neon_rgba(png_structrp png_ptr
, png_row_infop row_info
,
67 png_const_bytep row
, png_bytepp ssp
, png_bytepp ddp
)
69 png_uint_32 row_width
= row_info
->width
;
70 const png_uint_32
*riffled_palette
=
71 (const png_uint_32
*)png_ptr
->riffled_palette
;
72 const png_int_32 pixels_per_chunk
= 4;
75 if (row_width
< pixels_per_chunk
)
78 /* This function originally gets the last byte of the output row.
79 * The NEON part writes forward from a given position, so we have
80 * to seek this back by 4 pixels x 4 bytes.
82 *ddp
= *ddp
- ((pixels_per_chunk
* sizeof(png_uint_32
)) - 1);
84 for (i
= 0; i
< row_width
; i
+= pixels_per_chunk
)
87 png_bytep sp
= *ssp
- i
, dp
= *ddp
- (i
<< 2);
88 cur
= vld1q_dup_u32 (riffled_palette
+ *(sp
- 3));
89 cur
= vld1q_lane_u32(riffled_palette
+ *(sp
- 2), cur
, 1);
90 cur
= vld1q_lane_u32(riffled_palette
+ *(sp
- 1), cur
, 2);
91 cur
= vld1q_lane_u32(riffled_palette
+ *(sp
- 0), cur
, 3);
92 vst1q_u32((void *)dp
, cur
);
96 /* Remove the amount that wasn't processed. */
97 i
-= pixels_per_chunk
;
100 /* Decrement output pointers. */
102 *ddp
= *ddp
- (i
<< 2);
106 /* Expands a palettized row into RGB format. */
108 png_do_expand_palette_neon_rgb(png_structrp png_ptr
, png_row_infop row_info
,
109 png_const_bytep row
, png_bytepp ssp
, png_bytepp ddp
)
111 png_uint_32 row_width
= row_info
->width
;
112 png_const_bytep palette
= (png_const_bytep
)png_ptr
->palette
;
113 const png_uint_32 pixels_per_chunk
= 8;
116 if (row_width
<= pixels_per_chunk
)
119 /* Seeking this back by 8 pixels x 3 bytes. */
120 *ddp
= *ddp
- ((pixels_per_chunk
* sizeof(png_color
)) - 1);
122 for (i
= 0; i
< row_width
; i
+= pixels_per_chunk
)
125 png_bytep sp
= *ssp
- i
, dp
= *ddp
- ((i
<< 1) + i
);
126 cur
= vld3_dup_u8(palette
+ sizeof(png_color
) * (*(sp
- 7)));
127 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 6)), cur
, 1);
128 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 5)), cur
, 2);
129 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 4)), cur
, 3);
130 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 3)), cur
, 4);
131 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 2)), cur
, 5);
132 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 1)), cur
, 6);
133 cur
= vld3_lane_u8(palette
+ sizeof(png_color
) * (*(sp
- 0)), cur
, 7);
134 vst3_u8((void *)dp
, cur
);
139 /* Remove the amount that wasn't processed. */
140 i
-= pixels_per_chunk
;
143 /* Decrement output pointers. */
145 *ddp
= *ddp
- ((i
<< 1) + i
);
149 #endif /* PNG_ARM_NEON_IMPLEMENTATION */