| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
|
| 18 |
#include <fstream> |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
#include <math/vec3.h> |
| 22 |
|
| 23 |
#include <imageio/ImageDecoder.h> |
| 24 |
#include <imageio/ImageEncoder.h> |
| 25 |
|
| 26 |
#include <utils/Path.h> |
| 27 |
|
| 28 |
#include <getopt/getopt.h> |
| 29 |
|
| 30 |
using namespace filament::math; |
| 31 |
using namespace image; |
| 32 |
|
| 33 |
static image::ImageEncoder::Format g_format = image::ImageEncoder::Format::PNG; |
| 34 |
static bool g_formatSpecified = false; |
| 35 |
static std::string g_compression = ""; |
| 36 |
|
| 37 |
static void blend(const LinearImage& normal, const LinearImage& detail, LinearImage output); |
| 38 |
|
| 39 |
static void printUsage(const char* name) { |
| 40 |
std::string execName(utils::Path(name).getName()); |
| 41 |
std::string usage( |
| 42 |
"NORMALBLENDING is a tool for blending normal maps using Reoriented Normal Mapping\n" |
| 43 |
"Usage:\n" |
| 44 |
" NORMALBLENDING [options] <normal-map> <detail-map> <output-map>\n" |
| 45 |
"\n" |
| 46 |
"Supported input formats:\n" |
| 47 |
" PNG, 8 and 16 bits\n" |
| 48 |
" Radiance (.hdr)\n" |
| 49 |
" Photoshop (.psd), 16 and 32 bits\n" |
| 50 |
" OpenEXR (.exr)\n" |
| 51 |
"\n" |
| 52 |
"Options:\n" |
| 53 |
" --help, -h\n" |
| 54 |
" print this message\n\n" |
| 55 |
" --license\n" |
| 56 |
" Print copyright and license information\n\n" |
| 57 |
" --format=[exr|hdr|psd|png|dds], -f [exr|hdr|psd|png|dds]\n" |
| 58 |
" specify output file format, inferred from file name if omitted\n\n" |
| 59 |
" --compression=COMPRESSION, -c COMPRESSION\n" |
| 60 |
" format specific compression:\n" |
| 61 |
" PNG: Ignored\n" |
| 62 |
" Radiance: Ignored\n" |
| 63 |
" Photoshop: 16 (default), 32\n" |
| 64 |
" OpenEXR: RAW, RLE, ZIPS, ZIP, PIZ (default)\n" |
| 65 |
" DDS: 8, 16 (default), 32\n\n" |
| 66 |
); |
| 67 |
|
| 68 |
const std::string from("NORMALBLENDING"); |
| 69 |
for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) { |
| 70 |
usage.replace(pos, from.length(), execName); |
| 71 |
} |
| 72 |
printf("%s", usage.c_str()); |
| 73 |
} |
| 74 |
|
| 75 |
static void license() { |
| 76 |
static const char *license[] = { |
| 77 |
#include "licenses/licenses.inc" |
| 78 |
nullptr |
| 79 |
}; |
| 80 |
|
| 81 |
const char **p = &license[0]; |
| 82 |
while (*p) |
| 83 |
std::cout << *p++ << std::endl; |
| 84 |
} |
| 85 |
|
| 86 |
static int handleArguments(int argc, char* argv[]) { |
| 87 |
static constexpr const char* OPTSTR = "hf:c:"; |
| 88 |
static const struct option OPTIONS[] = { |
| 89 |
{ "help", no_argument, 0, 'h' }, |
| 90 |
{ "license", no_argument, 0, 'l' }, |
| 91 |
{ "format", required_argument, 0, 'f' }, |
| 92 |
{ "compression", required_argument, 0, 'c' }, |
| 93 |
{ 0, 0, 0, 0 } // termination of the option list |
| 94 |
}; |
| 95 |
|
| 96 |
int opt; |
| 97 |
int optionIndex = 0; |
| 98 |
|
| 99 |
while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &optionIndex)) >= 0) { |
| 100 |
std::string arg(optarg ? optarg : ""); |
| 101 |
switch (opt) { |
| 102 |
default: |
| 103 |
case 'h': |
| 104 |
printUsage(argv[0]); |
| 105 |
exit(0); |
| 106 |
// break; |
| 107 |
case 'l': |
| 108 |
license(); |
| 109 |
exit(0); |
| 110 |
// break; |
| 111 |
case 'f': |
| 112 |
if (arg == "png") { |
| 113 |
g_format = ImageEncoder::Format::PNG; |
| 114 |
g_formatSpecified = true; |
| 115 |
} |
| 116 |
if (arg == "hdr") { |
| 117 |
g_format = ImageEncoder::Format::HDR; |
| 118 |
g_formatSpecified = true; |
| 119 |
} |
| 120 |
if (arg == "exr") { |
| 121 |
g_format = ImageEncoder::Format::EXR; |
| 122 |
g_formatSpecified = true; |
| 123 |
} |
| 124 |
if (arg == "psd") { |
| 125 |
g_format = ImageEncoder::Format::PSD; |
| 126 |
g_formatSpecified = true; |
| 127 |
} |
| 128 |
if (arg == "dds") { |
| 129 |
g_format = ImageEncoder::Format::DDS_LINEAR; |
| 130 |
g_formatSpecified = true; |
| 131 |
} |
| 132 |
break; |
| 133 |
case 'c': |
| 134 |
g_compression = arg; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return optind; |
| 140 |
} |
| 141 |
|
| 142 |
int main(int argc, char* argv[]) { |
| 143 |
int optionIndex = handleArguments(argc, argv); |
| 144 |
|
| 145 |
int numArgs = argc - optionIndex; |
| 146 |
if (numArgs < 3) { |
| 147 |
printUsage(argv[0]); |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
utils::Path normalMap(argv[optionIndex ]); |
| 152 |
utils::Path detailMap(argv[optionIndex + 1]); |
| 153 |
utils::Path outputMap(argv[optionIndex + 2]); |
| 154 |
|
| 155 |
if (!normalMap.exists()) { |
| 156 |
std::cerr << "The input normal map does not exist: " << normalMap << std::endl; |
| 157 |
exit(1); |
| 158 |
} |
| 159 |
|
| 160 |
if (!detailMap.exists()) { |
| 161 |
std::cerr << "The input detail map does not exist: " << detailMap << std::endl; |
| 162 |
exit(1); |
| 163 |
} |
| 164 |
|
| 165 |
// make sure we load the normal maps as linear data |
| 166 |
std::ifstream inputStream(normalMap, std::ios::binary); |
| 167 |
LinearImage normalImage = ImageDecoder::decode(inputStream, normalMap, |
| 168 |
ImageDecoder::ColorSpace::LINEAR); |
| 169 |
if (!normalImage.isValid()) { |
| 170 |
std::cerr << "The input normal map is invalid: " << normalMap << std::endl; |
| 171 |
exit(1); |
| 172 |
} |
| 173 |
inputStream.close(); |
| 174 |
|
| 175 |
inputStream.open(detailMap, std::ios::binary); |
| 176 |
LinearImage detailImage = ImageDecoder::decode(inputStream, detailMap, |
| 177 |
ImageDecoder::ColorSpace::LINEAR); |
| 178 |
if (!detailImage.isValid()) { |
| 179 |
std::cerr << "The detail normal map is invalid: " << detailMap << std::endl; |
| 180 |
exit(1); |
| 181 |
} |
| 182 |
inputStream.close(); |
| 183 |
|
| 184 |
// TODO: handle normal maps of different sizes |
| 185 |
if (normalImage.getWidth() != detailImage.getWidth() || |
| 186 |
normalImage.getHeight() != detailImage.getHeight()) { |
| 187 |
std::cerr << "The normal and detail maps must have the same dimensions:" << std::endl |
| 188 |
<< " Normal map: " << normalImage.getWidth() << "x" << normalImage.getHeight() |
| 189 |
<< std::endl |
| 190 |
<< " Detail map: " << detailImage.getWidth() << "x" << detailImage.getHeight() |
| 191 |
<< std::endl; |
| 192 |
exit(1); |
| 193 |
} |
| 194 |
|
| 195 |
size_t width = normalImage.getWidth(); |
| 196 |
size_t height = normalImage.getHeight(); |
| 197 |
LinearImage image(width, height, 3); |
| 198 |
|
| 199 |
blend(normalImage, detailImage, image); |
| 200 |
|
| 201 |
if (!g_formatSpecified) { |
| 202 |
g_format = ImageEncoder::chooseFormat(outputMap); |
| 203 |
} |
| 204 |
|
| 205 |
std::ofstream outputStream(outputMap, std::ios::binary | std::ios::trunc); |
| 206 |
if (!outputStream.good()) { |
| 207 |
std::cerr << "The output file cannot be opened: " << outputMap << std::endl; |
| 208 |
exit(1); |
| 209 |
} |
| 210 |
|
| 211 |
ImageEncoder::encode(outputStream, g_format, image, g_compression, outputMap.getPath()); |
| 212 |
|
| 213 |
outputStream.close(); |
| 214 |
if (!outputStream.good()) { |
| 215 |
std::cerr << "An error occurred while writing the output file: " << outputMap << std::endl; |
| 216 |
exit(1); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void blend(const LinearImage& normal, const LinearImage& detail, LinearImage output) { |
| 221 |
const size_t width = output.getWidth(); |
| 222 |
const size_t height = output.getHeight(); |
| 223 |
|
| 224 |
for (size_t y = 0; y < height; y++) { |
| 225 |
auto normalRow = normal.get<float3>(0, y); |
| 226 |
auto detailRow = detail.get<float3>(0, y); |
| 227 |
auto outputRow = output.get<float3>(0, y); |
| 228 |
|
| 229 |
for (size_t x = 0; x < width; x++, normalRow++, detailRow++, outputRow++) { |
| 230 |
// Reoriented Normal Mapping |
| 231 |
float3 t = *normalRow * float3( 2, 2, 2) + float3(-1, -1, 0); |
| 232 |
float3 u = *detailRow * float3(-2, -2, 2) + float3( 1, 1, -1); |
| 233 |
float3 r = normalize(t * dot(t, u) - u * t.z); |
| 234 |
|
| 235 |
*outputRow = r * 0.5 + 0.5; |
| 236 |
} |
| 237 |
} |
| 238 |
} |