Skip to content

Commit

Permalink
Add HDR format support to _SAVEIMAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
a740g committed Sep 20, 2023
1 parent 9d3cafd commit 45e5a24
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions internal/c/parts/video/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ int32_t func__loadimage(qbs *qbsFileName, int32_t bpp, qbs *qbsRequirements, int
/// @param qbsRequirements Optional: Extra format and setting arguments
/// @param passed Argument bitmask
void sub__saveimage(qbs *qbsFileName, int32_t imageHandle, qbs *qbsRequirements, int32_t passed) {
enum struct SaveFormat { PNG = 0, QOI, BMP, TGA, JPG };
static const char *formatName[] = {"png", "qoi", "bmp", "tga", "jpg"};
enum struct SaveFormat { PNG = 0, QOI, BMP, TGA, JPG, HDR };
static const char *formatName[] = {"png", "qoi", "bmp", "tga", "jpg", "hdr"};

if (new_error) // leave if there was an error
return;
Expand Down Expand Up @@ -823,7 +823,7 @@ void sub__saveimage(qbs *qbsFileName, int32_t imageHandle, qbs *qbsRequirements,
}

++c; // move to the attribute
fc = (*c) & 0x0F;
fc = *c & 0x0F;
bc = ((*c >> 4) & 7) + ((*c >> 7) << 3);

// Inner codepoint rendering loop
Expand Down Expand Up @@ -908,6 +908,32 @@ void sub__saveimage(qbs *qbsFileName, int32_t imageHandle, qbs *qbsRequirements,
}
} break;

case SaveFormat::HDR: {
IMAGE_DEBUG_PRINT("Converting RGBA to linear float data");

const auto HDRComponents = 4;

std::vector<float> HDRPixels;
HDRPixels.resize(pixels.size() * HDRComponents);

size_t j = 0;
for (size_t i = 0; i < pixels.size(); i++) {
HDRPixels[j] = pow((pixels[i] & 0xFFu) / 255.0f, 2.2f);
++j;
HDRPixels[j] = pow(((pixels[i] >> 8) & 0xFFu) / 255.0f, 2.2f);
++j;
HDRPixels[j] = pow(((pixels[i] >> 16) & 0xFFu) / 255.0f, 2.2f);
++j;
HDRPixels[j] = (pixels[i] >> 24) / 255.0f;
++j;
}

if (!stbi_write_hdr(fileName.c_str(), width, height, HDRComponents, HDRPixels.data())) {
IMAGE_DEBUG_PRINT("stbi_write_hdr() failed");
error(ERROR_ILLEGAL_FUNCTION_CALL);
}
} break;

default:
IMAGE_DEBUG_PRINT("Save handler not implemented");
error(ERROR_INTERNAL_ERROR);
Expand Down

0 comments on commit 45e5a24

Please sign in to comment.