1// SPDX-License-Identifier: Apache-2.0
2// ----------------------------------------------------------------------------
3// Copyright 2011-2023 Arm Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6// use this file except in compliance with the License. You may obtain a copy
7// of the License at:
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14// License for the specific language governing permissions and limitations
15// under the License.
16// ----------------------------------------------------------------------------
17
18/**
19 * @brief Functions for building the implementation of stb_image and tinyexr.
20 */
21
22#include <cstdlib>
23#include <cstdio>
24#include <fstream>
25#include <vector>
26
27#include "astcenccli_internal.h"
28
29// Configure the STB image write library build.
30#define STB_IMAGE_IMPLEMENTATION
31#define STB_IMAGE_WRITE_IMPLEMENTATION
32#define STBI_NO_GIF
33#define STBI_NO_PIC
34#define STBI_NO_PNM
35#define STBI_NO_PNG
36#define STBI_NO_PSD
37
38// Configure the TinyEXR library build.
39#define TINYEXR_IMPLEMENTATION
40
41// Configure the Wuffs library build.
42#define WUFFS_IMPLEMENTATION
43#define WUFFS_CONFIG__MODULES
44#define WUFFS_CONFIG__MODULE__ADLER32
45#define WUFFS_CONFIG__MODULE__BASE
46#define WUFFS_CONFIG__MODULE__CRC32
47#define WUFFS_CONFIG__MODULE__DEFLATE
48#define WUFFS_CONFIG__MODULE__PNG
49#define WUFFS_CONFIG__MODULE__ZLIB
50#include "wuffs-v0.3.c"
51
52// For both libraries force asserts (which can be triggered by corrupt input
53// images) to be handled at runtime in release builds to avoid security issues.
54#define STBI_ASSERT(x) astcenc_runtime_assert(x)
55#define TEXR_ASSERT(x) astcenc_runtime_assert(x)
56
57/**
58 * @brief Trap image load failures and convert into a runtime error.
59 */
60static void astcenc_runtime_assert(bool condition)
61{
62    if (!condition)
63    {
64        print_error("ERROR: Corrupt input image\n");
65        exit(1);
66    }
67}
68
69#include "stb_image.h"
70#include "stb_image_write.h"
71#include "tinyexr.h"
72
73/**
74 * @brief Load an image using Wuffs to provide the loader.
75 *
76 * @param      filename          The name of the file to load.
77 * @param      y_flip            Should the image be vertically flipped?
78 * @param[out] is_hdr            Is this an HDR image load?
79 * @param[out] component_count   The number of components in the data.
80 *
81 * @return The loaded image data in a canonical 4 channel format, or @c nullptr on error.
82 */
83astcenc_image* load_png_with_wuffs(
84	const char* filename,
85	bool y_flip,
86	bool& is_hdr,
87	unsigned int& component_count
88) {
89	is_hdr = false;
90	component_count = 4;
91
92	std::ifstream file(filename, std::ios::binary | std::ios::ate);
93	if (!file)
94	{
95		print_error("ERROR: Failed to load image %s (can't fopen)\n", filename);
96		return nullptr;
97	}
98
99	std::streamsize size = file.tellg();
100	file.seekg(0, std::ios::beg);
101
102	std::vector<uint8_t> buffer(size);
103	file.read((char*)buffer.data(), size);
104
105	wuffs_png__decoder *dec = wuffs_png__decoder__alloc();
106	if (!dec)
107	{
108		return nullptr;
109	}
110
111	wuffs_base__image_config ic;
112	wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(buffer.data(), size, true);
113	wuffs_base__status status = wuffs_png__decoder__decode_image_config(dec, &ic, &src);
114	if (status.repr)
115	{
116		return nullptr;
117	}
118
119	uint32_t dim_x = wuffs_base__pixel_config__width(&ic.pixcfg);
120	uint32_t dim_y = wuffs_base__pixel_config__height(&ic.pixcfg);
121	size_t num_pixels = dim_x * dim_y;
122	if (num_pixels > (SIZE_MAX / 4))
123	{
124		return nullptr;
125	}
126
127	// Override the image's native pixel format to be RGBA_NONPREMUL
128	wuffs_base__pixel_config__set(
129	    &ic.pixcfg,
130	    WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL,
131	    WUFFS_BASE__PIXEL_SUBSAMPLING__NONE,
132	    dim_x, dim_y);
133
134	// Configure the work buffer
135	size_t workbuf_len = wuffs_png__decoder__workbuf_len(dec).max_incl;
136	if (workbuf_len > SIZE_MAX)
137	{
138		return nullptr;
139	}
140
141	wuffs_base__slice_u8 workbuf_slice = wuffs_base__make_slice_u8((uint8_t*)malloc(workbuf_len), workbuf_len);
142	if (!workbuf_slice.ptr)
143	{
144		return nullptr;
145	}
146
147	wuffs_base__slice_u8 pixbuf_slice = wuffs_base__make_slice_u8((uint8_t*)malloc(num_pixels * 4), num_pixels * 4);
148	if (!pixbuf_slice.ptr)
149	{
150		return nullptr;
151	}
152
153	wuffs_base__pixel_buffer pb;
154	status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, pixbuf_slice);
155	if (status.repr)
156	{
157		return nullptr;
158	}
159
160	// Decode the pixels
161	status = wuffs_png__decoder__decode_frame(dec, &pb, &src, WUFFS_BASE__PIXEL_BLEND__SRC, workbuf_slice, NULL);
162	if (status.repr)
163	{
164		return nullptr;
165	}
166
167	astcenc_image* img = astc_img_from_unorm8x4_array(pixbuf_slice.ptr, dim_x, dim_y, y_flip);
168
169	free(pixbuf_slice.ptr);
170	free(workbuf_slice.ptr);
171	free(dec);
172
173	return img;
174}
175