Line | Count | Source |
1 | | /* pngsimd.c - hardware (cpu/arch) specific code |
2 | | * |
3 | | * Copyright (c) 2018-2024 Cosmin Truta |
4 | | * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson |
5 | | * Copyright (c) 1996-1997 Andreas Dilger |
6 | | * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. |
7 | | * |
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 |
11 | | */ |
12 | | #include "pngpriv.h" |
13 | | |
14 | | #ifdef PNG_TARGET_CODE_IMPLEMENTATION |
15 | | /* This is set by pngtarget.h iff there is some target code to be compiled. |
16 | | */ |
17 | | |
18 | | /* Each piece of separate hardware support code must have a "init" file defined |
19 | | * in PNG_TARGET_CODE_IMPLEMENTATION and included here. |
20 | | * |
21 | | * The "check" header set PNG_TARGET_CODE_IMPLEMENTATION and that file *MUST* |
22 | | * supply macro definitions as follows. Note that all functions must be static |
23 | | * to avoid clashes with other implementations. |
24 | | * |
25 | | * png_target_impl |
26 | | * string constant |
27 | | * REQUIRED |
28 | | * This must be a string naming the implemenation. |
29 | | * |
30 | | * png_target_free_data_impl |
31 | | * static void png_target_free_data_impl(png_struct *) |
32 | | * REQUIRED if PNG_TARGET_STORES_DATA is defined |
33 | | * UNDEFINED if PNG_TARGET_STORES_DATA is not defined |
34 | | * A function to free data stored in png_struct::target_data. |
35 | | * |
36 | | * png_target_init_filter_functions_impl [flag: png_target_filters] |
37 | | * OPTIONAL |
38 | | * Contains code to overwrite the png_struct::read_filter array, see |
39 | | * the definition of png_init_filter_functions. Need not be defined, |
40 | | * only called if target_state contains png_target_filters. |
41 | | * |
42 | | * png_target_do_expand_palette_impl [flag: png_target_expand_palette] |
43 | | * static function |
44 | | * OPTIONAL |
45 | | * Handles the transform. Need not be defined, only called if the |
46 | | * state contains png_target_<transform>, may set this flag to zero, may |
47 | | * return false to indicate that the transform was not done (so the |
48 | | * C implementation must then execute). |
49 | | * |
50 | | * Note that pngtarget.h verifies that at least one thing is implemented, the |
51 | | * checks below ensure that the corresponding _impl macro is defined. |
52 | | */ |
53 | | |
54 | | /* This will fail in an obvious way with a meaningful error message if the file |
55 | | * does not exist: |
56 | | */ |
57 | | #include PNG_TARGET_CODE_IMPLEMENTATION |
58 | | |
59 | | #ifndef png_target_impl |
60 | | # error TARGET SPECIFIC CODE: PNG_TARGET_CODE_IMPLEMENTATION is defined but\ |
61 | | png_hareware_impl is not |
62 | | #endif |
63 | | |
64 | | #if defined(PNG_TARGET_STORES_DATA) != defined(png_target_free_data_impl) |
65 | | # error TARGET SPECIFIC CODE: png_target_free_data_impl unexpected setting |
66 | | #endif |
67 | | |
68 | | #if defined(PNG_TARGET_IMPLEMENTS_FILTERS) !=\ |
69 | | defined(png_target_init_filter_functions_impl) |
70 | | # error TARGET SPECIFIC CODE: png_target_init_filter_functions_impl unexpected\ |
71 | | setting |
72 | | #endif |
73 | | |
74 | | #if defined(PNG_TARGET_IMPLEMENTS_EXPAND_PALETTE) !=\ |
75 | | defined(png_target_do_expand_palette_impl) |
76 | | # error TARGET SPECIFIC CODE: png_target_do_expand_palette_impl unexpected\ |
77 | | setting |
78 | | #endif |
79 | | |
80 | | void |
81 | | png_target_init(png_struct *pp) |
82 | 76.0k | { |
83 | | /* Initialize png_struct::target_state if required. */ |
84 | 76.0k | # ifdef png_target_init_filter_functions_impl |
85 | 76.0k | # define PNG_TARGET_FILTER_SUPPORT png_target_filters |
86 | | # else |
87 | | # define PNG_TARGET_FILTER_SUPPORT 0U |
88 | | # endif |
89 | | # ifdef png_target_do_expand_palette_impl |
90 | | # define PNG_TARGET_EXPAND_PALETTE_SUPPORT png_target_expand_palette |
91 | | # else |
92 | 76.0k | # define PNG_TARGET_EXPAND_PALETTE_SUPPORT 0U |
93 | 76.0k | # endif |
94 | | |
95 | 76.0k | # define PNG_TARGET_SUPPORT (PNG_TARGET_FILTER_SUPPORT |\ |
96 | 76.0k | PNG_TARGET_EXPAND_PALETTE_SUPPORT) |
97 | | |
98 | 76.0k | # if PNG_TARGET_SUPPORT != 0U |
99 | 76.0k | pp->target_state = PNG_TARGET_SUPPORT; |
100 | | # else |
101 | | PNG_UNUSED(pp); |
102 | | # endif |
103 | 76.0k | } |
104 | | |
105 | | #ifdef PNG_TARGET_STORES_DATA |
106 | | void |
107 | | png_target_free_data(png_struct *pp) |
108 | | { |
109 | | /* Free any data allocated in the png_struct::target_data. |
110 | | */ |
111 | | if (pp->target_data != NULL) |
112 | | { |
113 | | png_target_free_data_impl(pp); |
114 | | if (pp->target_data != NULL) |
115 | | png_error(pp, png_target_impl ": allocated data not released"); |
116 | | } |
117 | | } |
118 | | #endif |
119 | | |
120 | | #ifdef PNG_TARGET_IMPLEMENTS_FILTERS |
121 | | void |
122 | | png_target_init_filter_functions(png_struct *pp, unsigned int bpp) |
123 | 658 | { |
124 | 658 | if (((pp->options >> PNG_TARGET_SPECIFIC_CODE) & 3) == PNG_OPTION_ON && |
125 | 658 | (pp->target_state & png_target_filters) != 0) |
126 | 658 | png_target_init_filter_functions_impl(pp, bpp); |
127 | 658 | } |
128 | | #endif /* filters */ |
129 | | |
130 | | #ifdef PNG_TARGET_IMPLEMENTS_EXPAND_PALETTE |
131 | | int |
132 | | png_target_do_expand_palette(png_struct *pp, png_row_info *rip) |
133 | | /*const png_byte *row, const png_byte **ssp, const png_byte **ddp) */ |
134 | | { |
135 | | /* This is exactly like 'png_do_expand_palette' except that there is a check |
136 | | * on the options and target_state: |
137 | | */ |
138 | | return ((pp->options >> PNG_TARGET_SPECIFIC_CODE) & 3) == PNG_OPTION_ON && |
139 | | (pp->target_state & png_target_expand_palette) != 0 && |
140 | | png_target_do_expand_palette_impl(pp, rip, pp->row_buf + 1, |
141 | | pp->palette, pp->trans_alpha, pp->num_trans); |
142 | | } |
143 | | #endif /* EXPAND_PALETTE */ |
144 | | #endif /* PNG_TARGET_ARCH */ |