| 9158 | } |
| 9159 | |
| 9160 | int SaveEXR(const float *data, int width, int height, int components, |
| 9161 | const int save_as_fp16, const char *outfilename, const char **err) { |
| 9162 | if ((components == 1) || components == 3 || components == 4) { |
| 9163 | // OK |
| 9164 | } else { |
| 9165 | std::stringstream ss; |
| 9166 | ss << "Unsupported component value : " << components << std::endl; |
| 9167 | |
| 9168 | tinyexr::SetErrorMessage(ss.str(), err); |
| 9169 | return TINYEXR_ERROR_INVALID_ARGUMENT; |
| 9170 | } |
| 9171 | |
| 9172 | EXRHeader header; |
| 9173 | InitEXRHeader(&header); |
| 9174 | |
| 9175 | if ((width < 16) && (height < 16)) { |
| 9176 | // No compression for small image. |
| 9177 | header.compression_type = TINYEXR_COMPRESSIONTYPE_NONE; |
| 9178 | } else { |
| 9179 | header.compression_type = TINYEXR_COMPRESSIONTYPE_ZIP; |
| 9180 | } |
| 9181 | |
| 9182 | EXRImage image; |
| 9183 | InitEXRImage(&image); |
| 9184 | |
| 9185 | image.num_channels = components; |
| 9186 | |
| 9187 | std::vector<float> images[4]; |
| 9188 | const size_t pixel_count = |
| 9189 | static_cast<size_t>(width) * static_cast<size_t>(height); |
| 9190 | |
| 9191 | if (components == 1) { |
| 9192 | images[0].resize(pixel_count); |
| 9193 | memcpy(images[0].data(), data, sizeof(float) * pixel_count); |
| 9194 | } else { |
| 9195 | images[0].resize(pixel_count); |
| 9196 | images[1].resize(pixel_count); |
| 9197 | images[2].resize(pixel_count); |
| 9198 | images[3].resize(pixel_count); |
| 9199 | |
| 9200 | // Split RGB(A)RGB(A)RGB(A)... into R, G and B(and A) layers |
| 9201 | for (size_t i = 0; i < pixel_count; i++) { |
| 9202 | images[0][i] = data[static_cast<size_t>(components) * i + 0]; |
| 9203 | images[1][i] = data[static_cast<size_t>(components) * i + 1]; |
| 9204 | images[2][i] = data[static_cast<size_t>(components) * i + 2]; |
| 9205 | if (components == 4) { |
| 9206 | images[3][i] = data[static_cast<size_t>(components) * i + 3]; |
| 9207 | } |
| 9208 | } |
| 9209 | } |
| 9210 | |
| 9211 | float *image_ptr[4] = {0, 0, 0, 0}; |
| 9212 | if (components == 4) { |
| 9213 | image_ptr[0] = &(images[3].at(0)); // A |
| 9214 | image_ptr[1] = &(images[2].at(0)); // B |
| 9215 | image_ptr[2] = &(images[1].at(0)); // G |
| 9216 | image_ptr[3] = &(images[0].at(0)); // R |
| 9217 | } else if (components == 3) { |
nothing calls this directly
no test coverage detected