Default process image function. Has extra flipImageY argument that must be filled in.
| 132 | // Default process image function. |
| 133 | // Has extra flipImageY argument that must be filled in. |
| 134 | bool defaultProcessImageFunction(const vector<unsigned char>& buffer, const string& filename, |
| 135 | ImageAsset* pImageOut, bool flipImageY) |
| 136 | { |
| 137 | // Use STB to decode the image buffer. |
| 138 | // TODO: Image loading will eventually be handled by clients. |
| 139 | int width, height, components; |
| 140 | |
| 141 | // Is this image HDR? |
| 142 | bool isHDR = stbi_is_hdr_from_memory(&buffer[0], (int)buffer.size()); |
| 143 | |
| 144 | if (isHDR) |
| 145 | { |
| 146 | // Don't flip HDR images. |
| 147 | stbi_set_flip_vertically_on_load(false); |
| 148 | |
| 149 | // Load HDR image as floats. |
| 150 | float* pPixels = |
| 151 | stbi_loadf_from_memory(&buffer[0], (int)buffer.size(), &width, &height, &components, 0); |
| 152 | |
| 153 | AU_ASSERT( |
| 154 | components == 3 || components == 4, "Only RGB and RGBA HDR images currently supported"); |
| 155 | |
| 156 | // Fill in Aurora image data struct. |
| 157 | pImageOut->data.width = width; |
| 158 | pImageOut->data.height = height; |
| 159 | pImageOut->data.name = filename; |
| 160 | pImageOut->data.linearize = false; |
| 161 | pImageOut->data.format = components == 3 ? ImageFormat::Float_RGB : ImageFormat::Float_RGBA; |
| 162 | size_t sizeBytes = static_cast<size_t>(width * height * components * sizeof(float)); |
| 163 | pImageOut->pixels = make_unique<unsigned char[]>(sizeBytes); |
| 164 | pImageOut->data.pImageData = pImageOut->pixels.get(); |
| 165 | pImageOut->sizeBytes = sizeBytes; |
| 166 | |
| 167 | // Copy pixels. |
| 168 | memcpy(pImageOut->pixels.get(), pPixels, sizeBytes); |
| 169 | |
| 170 | // Free the pixels allocated by STB. |
| 171 | stbi_image_free(pPixels); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | |
| 176 | // Set the flipped flag based on static variable in AssetManager. |
| 177 | stbi_set_flip_vertically_on_load(flipImageY); |
| 178 | |
| 179 | // Load LDR image as bytes. |
| 180 | unsigned char* pPixels = |
| 181 | stbi_load_from_memory(&buffer[0], (int)buffer.size(), &width, &height, &components, 0); |
| 182 | |
| 183 | // Check if image is sRGB (if so set linearize flag.) |
| 184 | bool isSRGB = isImageSRGB(&buffer[0], (int)buffer.size()); |
| 185 | |
| 186 | // Fill in Aurora image data struct. |
| 187 | pImageOut->data.width = width; |
| 188 | pImageOut->data.height = height; |
| 189 | pImageOut->data.name = filename; |
| 190 | pImageOut->data.linearize = isSRGB; |
| 191 | pImageOut->data.format = ImageFormat::Integer_RGBA; |
no test coverage detected