Skip to content

Instantly share code, notes, and snippets.

@bgotink
Last active July 26, 2022 14:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgotink/635bca8e2a3d47bf6a5f to your computer and use it in GitHub Desktop.
Save bgotink/635bca8e2a3d47bf6a5f to your computer and use it in GitHub Desktop.
A small C++ program that converts grayscale EXR to transparent EXR

gray2transparent

This small C++ program converts a grayscale OpenEXR image to an image with transparancy.

behaviour

  • takes only red channel of RGBA into account, prints message if color isn't gray
  • ignores pixels with transparancy already set tot a non-opaque value
  • makes lighter colors more transparant: alpha = 1 - red

Licensing

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

#include "exr_io.h"
#include <cstdio>
#include <cstdlib>
#include <ImfInputFile.h>
#include <ImfChannelList.h>
#include <ImfFrameBuffer.h>
#include <ImfRgbaFile.h>
#include <half.h>
#include <cassert>
using namespace Imf;
using namespace Imath;
bool ReadEXR(const char *name, float *&rgba, int &xRes, int &yRes, bool &hasAlpha)
{
try {
InputFile file(name);
Box2i dw = file.header().dataWindow();
xRes = dw.max.x - dw.min.x + 1;
yRes = dw.max.y - dw.min.y + 1;
half *hrgba = new half[4 * xRes * yRes];
// for now...
hasAlpha = true;
int nChannels = 4;
hrgba -= 4 * (dw.min.x + dw.min.y * xRes);
FrameBuffer frameBuffer;
frameBuffer.insert("R", Slice(HALF, (char *)hrgba,
4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
frameBuffer.insert("G", Slice(HALF, (char *)hrgba+sizeof(half),
4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
frameBuffer.insert("B", Slice(HALF, (char *)hrgba+2*sizeof(half),
4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
frameBuffer.insert("A", Slice(HALF, (char *)hrgba+3*sizeof(half),
4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 1.0));
file.setFrameBuffer(frameBuffer);
file.readPixels(dw.min.y, dw.max.y);
hrgba += 4 * (dw.min.x + dw.min.y * xRes);
rgba = new float[nChannels * xRes * yRes];
for (int i = 0; i < nChannels * xRes * yRes; ++i)
rgba[i] = hrgba[i];
delete[] hrgba;
} catch (const std::exception &e) {
fprintf(stderr, "Unable to read image file \"%s\": %s", name, e.what());
return NULL;
}
return rgba;
}
void WriteEXR(const char *name, float *pixels, int xRes, int yRes) {
Rgba *hrgba = new Rgba[xRes * yRes];
for (int i = 0; i < xRes * yRes; ++i)
hrgba[i] = Rgba(pixels[4*i], pixels[4*i+1], pixels[4*i+2], pixels[4*i+3]);
Box2i displayWindow(V2i(0,0), V2i(xRes-1, yRes-1));
Box2i dataWindow = displayWindow;
RgbaOutputFile file(name, displayWindow, dataWindow, WRITE_RGBA);
file.setFrameBuffer(hrgba, 1, xRes);
try {
file.writePixels(yRes);
}
catch (const std::exception &e) {
fprintf(stderr, "Unable to write image file \"%s\": %s", name,
e.what());
}
delete[] hrgba;
}
#ifndef __EXR_IO_H
#define __EXR_IO_H
bool ReadEXR(const char *name, float *&rgba, int &xRes, int &yRes, bool &hasAlpha);
void WriteEXR(const char *name, float *pixels, int xRes, int yRes);
#endif // defined(__EXR_IO_H)
/*
*/
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include "exr_io.h"
static void usage(const char * const program) {
fprintf(stderr, "usage: %s <input.exr> <output.exr>\n", program);
exit(1);
}
int main(int argc, char *argv[])
{
const char *outfile = NULL;
const char *infile = NULL;
if (argc == 1) usage(argv[0]);
for (int i = 1; i < argc; ++i) {
if (!infile)
infile = argv[i];
else if (!outfile)
outfile = argv[i];
else
usage(argv[0]);
}
float *image;
int resolution[2];
bool hasAlpha;
if (!ReadEXR(infile, image, resolution[0], resolution[1], hasAlpha)) {
printf("couldn't read image %s\n", infile);
return 1;
}
assert(hasAlpha);
for (int i = 0; i < 4*resolution[0]*resolution[1]; i += 4) {
if (image[i+3] != 1)
// alpha channel already set, ignore pixel :)
continue;
float color = image[i];
if (image[i+1] != color || image[i+2] != color)
printf("Color (%f,%f,%f) isn't greyscale, taking only red channel into account",
color, image[i+1], image[i+2]);
// lighter = more transparent
image[i+3] = 1 - color;
}
WriteEXR(outfile, image, resolution[0], resolution[1]);
return 0;
}
.PRECIOUS: %.o
CXX=clang++
EXR_INCLUDES=-I/usr/local/include/OpenEXR -I/usr/include/OpenEXR -I/opt/local/include/OpenEXR
EXR_LIBDIR=-L/usr/local/lib -L/opt/local/lib
OPT=-o3
EXRLIBS=$(EXR_LIBDIR) -Bstatic -lIex -lIlmImf -lIlmThread -lImath -lIex -lHalf -Bdynamic
ARCH = $(shell uname)
ifeq ($(ARCH),Linux)
EXRLIBS += -lpthread
endif
ifeq ($(ARCH),OpenBSD)
EXRLIBS += -lpthread
endif
ifeq ($(ARCH),Darwin)
EXRLIBS += -lz
endif
INCLUDE=$(EXR_INCLUDES)
LIBS=$(EXR_LIBDIR) $(EXRLIBS) -lm
WARN=-Wall
CXXFLAGS=$(OPT) $(MARCH) $(INCLUDE) $(WARN) $(DEFS)
.PHONY: clean default
default: gray2transparent
clean: ; rm -f *.o gray2transparent
gray2transparent: gray2transparent.o exr_io.o
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -o $@ -c $<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment