Exemplo n.º 1
0
int main(int argc, char **argv)
{
    if(argc<5)
    {
        std::cerr << USAGE_TEXT << std::endl;
        exit(0);
    }

    const char * inputimage = argv[1];
    const char * inputcolorspace = argv[2];
    const char * outputimage = argv[3];
    const char * outputcolorspace = argv[4];

    OIIO::ImageSpec spec;
    std::vector<float> img;
    int imgwidth = 0;
    int imgheight = 0;
    int components = 0;

    // Load the image
    std::cerr << "Loading " << inputimage << std::endl;
    try
    {
        OIIO::ImageInput* f = OIIO::ImageInput::create(inputimage);
        if(!f)
        {
            std::cerr << "Could not create image input." << std::endl;
            exit(1);
        }

        f->open(inputimage, spec);

        std::string error = f->geterror();
        if(!error.empty())
        {
            std::cerr << "Error loading image " << error << std::endl;
            exit(1);
        }

        imgwidth = spec.width;
        imgheight = spec.height;
        components = spec.nchannels;

        img.resize(imgwidth*imgheight*components);
        memset(&img[0], 0, imgwidth*imgheight*components*sizeof(float));

        f->read_image(OIIO::TypeDesc::TypeFloat, &img[0]);
        delete f;

    }
    catch(...)
    {
        std::cerr << "Error loading file.";
        exit(1);
    }

    // Process the image
    try
    {
        // Load the current config.
        OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();

        // Get the processor
        OCIO::ConstProcessorRcPtr processor = config->getProcessor(inputcolorspace, outputcolorspace);

        // Wrap the image in a light-weight ImageDescription
        OCIO::PackedImageDesc imageDesc(&img[0], imgwidth, imgheight, components);

        // Apply the color transformation (in place)
        processor->apply(imageDesc);
    }
    catch(OCIO::Exception & exception)
    {
        std::cerr << "OCIO Error: " << exception.what() << std::endl;
    }
    catch(...)
    {
        std::cerr << "Unknown OCIO error encountered." << std::endl;
    }

    // Write out the result
    try
    {
        OIIO::ImageOutput* f = OIIO::ImageOutput::create(outputimage);
        if(!f)
        {
            std::cerr << "Could not create output input." << std::endl;
            exit(1);
        }

        f->open(outputimage, spec);
        f->write_image(OIIO::TypeDesc::FLOAT, &img[0]);
        f->close();
        delete f;
    }
    catch(...)
    {
        std::cerr << "Error loading file.";
        exit(1);
    }

    std::cerr << "Wrote " << outputimage << std::endl;

    return 0;
}
Exemplo n.º 2
0
void Extract(int cubesize, int maxwidth,
             const std::string & inputfile,
             const std::string & outputfile)
{
    // Read the image
    OIIO::ImageInput* f = OIIO::ImageInput::create(inputfile);
    if(!f)
    {
        throw Exception("Could not create input image.");
    }
    
    OIIO::ImageSpec spec;
    f->open(inputfile, spec);
    
    std::string error = f->geterror();
    if(!error.empty())
    {
        std::ostringstream os;
        os << "Error loading image " << error;
        throw Exception(os.str().c_str());
    }
    
    int width = 0;
    int height = 0;
    GetLutImageSize(width, height, cubesize, maxwidth);
    
    if(spec.width != width || spec.height != height)
    {
        std::ostringstream os;
        os << "Image does not have expected dimensions. ";
        os << "Expected " << width << "x" << height << ", ";
        os << "Found " << spec.width << "x" << spec.height;
        throw Exception(os.str().c_str());
    }
    
    if(spec.nchannels<3)
    {
        throw Exception("Image must have 3 or more channels.");
    }
    
    int lut3DNumPixels = cubesize*cubesize*cubesize;
    
    if(spec.width*spec.height<lut3DNumPixels)
    {
        throw Exception("Image is not large enough to contain expected 3dlut.");
    }
    
    // TODO: confirm no data window?
    std::vector<float> img;
    img.resize(spec.width*spec.height*spec.nchannels, 0);
    f->read_image(OIIO::TypeDesc::TypeFloat, &img[0]);
    delete f;
    
    // Repack into rgb
    // Convert the RGB[...] image to an RGB image, in place.
    // Of course, this only works because we're doing it from left to right
    // so old pixels are read before they're written over
    
    if(spec.nchannels > 3)
    {
        for(int i=0; i<lut3DNumPixels; ++i)
        {
            img[3*i+0] = img[spec.nchannels*i+0];
            img[3*i+1] = img[spec.nchannels*i+1];
            img[3*i+2] = img[spec.nchannels*i+2];
        }
    }
    
    img.resize(lut3DNumPixels*3);
    
    // Write the output lut
    WriteLut3D(outputfile, &img[0], cubesize);
}
Exemplo n.º 3
0
static void InitImageTexture(const char * filename)
{
    glGenTextures(1, &g_imageTexID);

    std::vector<float> img;
    int texWidth = 512;
    int texHeight = 512;
    int components = 4;

    if(filename)
    {
        std::cout << "loading: " << filename << std::endl;
        try
        {
            OIIO::ImageInput* f = OIIO::ImageInput::create(filename);
            if(!f)
            {
                std::cerr << "Could not create image input." << std::endl;
                exit(1);
            }

            OIIO::ImageSpec spec;
            f->open(filename, spec);

            std::string error = f->geterror();
            if(!error.empty())
            {
                std::cerr << "Error loading image " << error << std::endl;
                exit(1);
            }

            texWidth = spec.width;
            texHeight = spec.height;
            components = spec.nchannels;

            img.resize(texWidth*texHeight*components);
            memset(&img[0], 0, texWidth*texHeight*components*sizeof(float));

            f->read_image(OIIO::TypeDesc::TypeFloat, &img[0]);
            delete f;
        }
        catch(...)
        {
            std::cerr << "Error loading file.";
            exit(1);
        }
    }
    // If no file is provided, use a default gradient texture
    else
    {
        std::cout << "No image specified, loading gradient." << std::endl;

        img.resize(texWidth*texHeight*components);
        memset(&img[0], 0, texWidth*texHeight*components*sizeof(float));

        for(int y=0; y<texHeight; ++y)
        {
            for(int x=0; x<texWidth; ++x)
            {
                float c = (float)x/((float)texWidth-1.0f);
                img[components*(texWidth*y+x) + 0] = c;
                img[components*(texWidth*y+x) + 1] = c;
                img[components*(texWidth*y+x) + 2] = c;
                img[components*(texWidth*y+x) + 3] = 1.0f;
            }
        }
    }


    GLenum format = 0;
    if(components == 4) format = GL_RGBA;
    else if(components == 3) format = GL_RGB;
    else
    {
        std::cerr << "Cannot load image with " << components << " components." << std::endl;
        exit(1);
    }

    g_imageAspect = 1.0;
    if(texHeight!=0)
    {
        g_imageAspect = (float) texWidth / (float) texHeight;
    }

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, g_imageTexID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0,
                 format, GL_FLOAT, &img[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}