“Open and Save File”#

This sample program demonstrates how to use the ImageSDK, separately from the CameraSDK, to open an IIQ file, decompress it and save the decompressed data into a new file.

Full Source code#

#include <iostream> // Print out messages to the console
#include <fstream> // Save to file
#include <P1Camera.hpp> // CameraSDK
#include <P1Image.hpp> // ImageSDK

// Open a file and decompress into a new file
int main(int argc, const char** argv)
{
	// All calls to the ImageSDK must be wrapped in a try-catch, because all exceptions
	// are used to report errors, etc.
	try
	{
		// Parse IIQ file
		P1::ImageSdk::RawImage image("test_input.iiq");

		// Decode image data with default values
		P1::ImageSdk::DecodeConfig decodeConfig = P1::ImageSdk::DecodeConfig::Defaults;
		P1::ImageSdk::SensorBayerOutput decodedImage = image.Decode(decodeConfig);

		std::cout << "Decoded image size: " << decodedImage.ByteSize()
			<< " Height=" << decodedImage.FullHeight()
			<< " Width=" << decodedImage.FullWidth() << std::endl;

		// Write decoded bitmap to file
		std::fstream rawFile("bitmap.bin", std::ios::binary | std::ios::trunc |
			std::ios::out);
		rawFile.write((char*)decodedImage.Data().get(), decodedImage.ByteSize());
		rawFile.close();

		std::cout << "Program successfully completed" << std::endl;
		return 0;
	}
	catch (P1::ImageSdk::SdkException exception)
	{
		// Exception from ImageSDK
		std::cout << "Exception: " << exception.what() << " Code:" << exception.mCode
			<< std::endl;
		return 1;
	}
	catch (...)
	{
		// Any other exception - just in case
		std::cout << "Argh - we got an exception" << std::endl;
		return 1;
	}

}
using System;
using P1.CameraSdk;
using P1.ImageSdk;

namespace TestDocumentation2Cs
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                RawImage image = new RawImage("../../../test_input.iiq");

                // Decode image data with default values
                DecodeConfig decodeConfig = DecodeConfig.Defaults();
                SensorBayerOutput decodedImage = image.Decode(decodeConfig);

                Console.WriteLine("Decoded image size: {0}, Dimension: {1} x {2}",
                    decodedImage.ByteSize, decodedImage.FullWidth, decodedImage.FullHeight);

                // Write decoded bitmap to file
                System.IO.File.WriteAllBytes("bitmapBayer.bin", decodedImage.Data);
                return 0;
            }
            catch(P1.ImageSdk.SdkException e)
            {
                Console.WriteLine("Image SDK error {0} : {1}", e.Code, e.Message);
                return 1;
            }
            catch (Exception e)
            {
                Console.WriteLine("Output: Any other error: {0}", e.Message);
                return 1;
            }
        }
    }
}

Decode Methods#

When decoding the image through the ImageSDK Decode pipeline (see Decode Pipeline), it is possible to substitute the image.Decode(decodeConfig) method with the decodeConfig.ApplyTo(image) method, which yields the same result:

// Decode image data with default values
P1::ImageSdk::DecodeConfig decodeConfig = P1::ImageSdk::DecodeConfig::Defaults;
P1::ImageSdk::SensorBayerOutput decodedImage = decodeConfig.ApplyTo(image);
// Decode image data with default values
DecodeConfig decodeConfig = DecodeConfig.Defaults();
SensorBayerOutput decodedImage = decodeConfig.ApplyTo(image);