bool DPXOutput::open (const std::string &name, const ImageSpec &userspec, OpenMode mode) { close (); // Close any already-opened file if (mode != Create) { error ("%s does not support subimages or MIP levels", format_name()); return false; } m_spec = userspec; // Stash the spec // open the image m_stream = new OutStream(); if (! m_stream->Open(name.c_str ())) { error ("Could not open file \"%s\"", name.c_str ()); return false; } // Check for things this format doesn't support if (m_spec.width < 1 || m_spec.height < 1) { error ("Image resolution must be at least 1x1, you asked for %d x %d", m_spec.width, m_spec.height); return false; } if (m_spec.depth < 1) m_spec.depth = 1; else if (m_spec.depth > 1) { error ("DPX does not support volume images (depth > 1)"); return false; } if (m_spec.format == TypeDesc::UINT8 || m_spec.format == TypeDesc::INT8) m_datasize = dpx::kByte; else if (m_spec.format == TypeDesc::UINT16 || m_spec.format == TypeDesc::INT16) m_datasize = dpx::kWord; else if (m_spec.format == TypeDesc::FLOAT || m_spec.format == TypeDesc::HALF) { m_spec.format = TypeDesc::FLOAT; m_datasize = dpx::kFloat; } else if (m_spec.format == TypeDesc::DOUBLE) m_datasize = dpx::kDouble; else { // use 16-bit unsigned integers as a failsafe m_spec.format = TypeDesc::UINT16; m_datasize = dpx::kWord; } // check if the client is giving us raw data to write m_wantRaw = m_spec.get_int_attribute ("dpx:RawData", 0) != 0; // check if the client wants endianness reverse to native // assume big endian per Jeremy's request, unless little endian is // explicitly specified std::string tmpstr = m_spec.get_string_attribute ("oiio:Endian", littleendian() ? "little" : "big"); m_wantSwap = (littleendian() != Strutil::iequals (tmpstr, "little")); m_dpx.SetOutStream (m_stream); // start out the file m_dpx.Start (); // some metadata std::string project = m_spec.get_string_attribute ("DocumentName", ""); std::string copyright = m_spec.get_string_attribute ("Copyright", ""); tmpstr = m_spec.get_string_attribute ("DateTime", ""); if (tmpstr.size () >= 19) { // libdpx's date/time format is pretty close to OIIO's (libdpx uses // %Y:%m:%d:%H:%M:%S%Z) // NOTE: the following code relies on the DateTime attribute being properly // formatted! // assume UTC for simplicity's sake, fix it if someone complains tmpstr[10] = ':'; tmpstr.replace (19, -1, "Z"); } m_dpx.SetFileInfo (name.c_str (), // filename tmpstr.c_str (), // cr. date OIIO_INTRO_STRING, // creator project.empty () ? NULL : project.c_str (), // project copyright.empty () ? NULL : copyright.c_str (), // copyright m_spec.get_int_attribute ("dpx:EncryptKey", ~0), // encryption key m_wantSwap); // image info m_dpx.SetImageInfo (m_spec.width, m_spec.height); // determine descriptor m_desc = get_descriptor_from_string (m_spec.get_string_attribute ("dpx:ImageDescriptor", "")); // transfer function dpx::Characteristic transfer; std::string colorspace = m_spec.get_string_attribute ("oiio:ColorSpace", ""); if (Strutil::iequals (colorspace, "Linear")) transfer = dpx::kLinear; else if (Strutil::iequals (colorspace, "GammaCorrected")) transfer = dpx::kUserDefined; else if (Strutil::iequals (colorspace, "Rec709")) transfer = dpx::kITUR709; else if (Strutil::iequals (colorspace, "KodakLog")) transfer = dpx::kLogarithmic; else { std::string dpxtransfer = m_spec.get_string_attribute ("dpx:Transfer", ""); transfer = get_characteristic_from_string (dpxtransfer); } // colorimetric m_cmetr = get_characteristic_from_string (m_spec.get_string_attribute ("dpx:Colorimetric", "User defined")); // select packing method dpx::Packing packing; tmpstr = m_spec.get_string_attribute ("dpx:Packing", "Filled, method A"); if (Strutil::iequals (tmpstr, "Packed")) packing = dpx::kPacked; else if (Strutil::iequals (tmpstr, "Filled, method B")) packing = dpx::kFilledMethodB; else packing = dpx::kFilledMethodA; // calculate target bit depth int bitDepth = m_spec.format.size () * 8; if (m_spec.format == TypeDesc::UINT16) { bitDepth = m_spec.get_int_attribute ("oiio:BitsPerSample", 16); if (bitDepth != 10 && bitDepth != 12 && bitDepth != 16) { error ("Unsupported bit depth %d", bitDepth); return false; } } m_dpx.header.SetBitDepth (0, bitDepth); // Bug workaround: libDPX doesn't appear to correctly support // "filled method A" for 12 bit data. Does anybody care what // packing/filling we use? Punt and just use "packed". if (bitDepth == 12) packing = dpx::kPacked; // see if we'll need to convert or not if (m_desc == dpx::kRGB || m_desc == dpx::kRGBA) { // shortcut for RGB(A) that gets the job done m_bytes = m_spec.scanline_bytes (); m_wantRaw = true; } else { m_bytes = dpx::QueryNativeBufferSize (m_desc, m_datasize, m_spec.width, 1); if (m_bytes == 0 && !m_wantRaw) { error ("Unable to deliver native format data from source data"); return false; } else if (m_bytes < 0) { // no need to allocate another buffer if (!m_wantRaw) m_bytes = m_spec.scanline_bytes (); else m_bytes = -m_bytes; } } if (m_bytes < 0) m_bytes = -m_bytes; m_dpx.SetElement (0, m_desc, bitDepth, transfer, m_cmetr, packing, dpx::kNone, (m_spec.format == TypeDesc::INT8 || m_spec.format == TypeDesc::INT16) ? 1 : 0, m_spec.get_int_attribute ("dpx:LowData", 0xFFFFFFFF), m_spec.get_float_attribute ("dpx:LowQuantity", std::numeric_limits<float>::quiet_NaN()), m_spec.get_int_attribute ("dpx:HighData", 0xFFFFFFFF), m_spec.get_float_attribute ("dpx:HighQuantity", std::numeric_limits<float>::quiet_NaN()), m_spec.get_int_attribute ("dpx:EndOfLinePadding", 0), m_spec.get_int_attribute ("dpx:EndOfImagePadding", 0)); m_dpx.header.SetXScannedSize (m_spec.get_float_attribute ("dpx:XScannedSize", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetYScannedSize (m_spec.get_float_attribute ("dpx:YScannedSize", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetFramePosition (m_spec.get_int_attribute ("dpx:FramePosition", 0xFFFFFFFF)); m_dpx.header.SetSequenceLength (m_spec.get_int_attribute ("dpx:SequenceLength", 0xFFFFFFFF)); m_dpx.header.SetHeldCount (m_spec.get_int_attribute ("dpx:HeldCount", 0xFFFFFFFF)); m_dpx.header.SetFrameRate (m_spec.get_float_attribute ("dpx:FrameRate", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetShutterAngle (m_spec.get_float_attribute ("dpx:ShutterAngle", std::numeric_limits<float>::quiet_NaN())); // FIXME: should we write the input version through or always default to 2.0? /*tmpstr = m_spec.get_string_attribute ("dpx:Version", ""); if (tmpstr.size () > 0) m_dpx.header.SetVersion (tmpstr.c_str ());*/ tmpstr = m_spec.get_string_attribute ("dpx:Format", ""); if (tmpstr.size () > 0) m_dpx.header.SetFormat (tmpstr.c_str ()); tmpstr = m_spec.get_string_attribute ("dpx:FrameId", ""); if (tmpstr.size () > 0) m_dpx.header.SetFrameId (tmpstr.c_str ()); tmpstr = m_spec.get_string_attribute ("dpx:SlateInfo", ""); if (tmpstr.size () > 0) m_dpx.header.SetSlateInfo (tmpstr.c_str ()); tmpstr = m_spec.get_string_attribute ("dpx:SourceImageFileName", ""); if (tmpstr.size () > 0) m_dpx.header.SetSourceImageFileName (tmpstr.c_str ()); tmpstr = m_spec.get_string_attribute ("dpx:InputDevice", ""); if (tmpstr.size () > 0) m_dpx.header.SetInputDevice (tmpstr.c_str ()); tmpstr = m_spec.get_string_attribute ("dpx:InputDeviceSerialNumber", ""); if (tmpstr.size () > 0) m_dpx.header.SetInputDeviceSerialNumber (tmpstr.c_str ()); m_dpx.header.SetInterlace (m_spec.get_int_attribute ("dpx:Interlace", 0xFF)); m_dpx.header.SetFieldNumber (m_spec.get_int_attribute ("dpx:FieldNumber", 0xFF)); m_dpx.header.SetHorizontalSampleRate (m_spec.get_float_attribute ("dpx:HorizontalSampleRate", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetVerticalSampleRate (m_spec.get_float_attribute ("dpx:VerticalSampleRate", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetTemporalFrameRate (m_spec.get_float_attribute ("dpx:TemporalFrameRate", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetTimeOffset (m_spec.get_float_attribute ("dpx:TimeOffset", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetBlackLevel (m_spec.get_float_attribute ("dpx:BlackLevel", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetBlackGain (m_spec.get_float_attribute ("dpx:BlackGain", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetBreakPoint (m_spec.get_float_attribute ("dpx:BreakPoint", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetWhiteLevel (m_spec.get_float_attribute ("dpx:WhiteLevel", std::numeric_limits<float>::quiet_NaN())); m_dpx.header.SetIntegrationTimes (m_spec.get_float_attribute ("dpx:IntegrationTimes", std::numeric_limits<float>::quiet_NaN())); float aspect = m_spec.get_float_attribute ("PixelAspectRatio", 1.0f); int aspect_num, aspect_den; float_to_rational (aspect, aspect_num, aspect_den); m_dpx.header.SetAspectRatio (0, aspect_num); m_dpx.header.SetAspectRatio (1, aspect_den); tmpstr = m_spec.get_string_attribute ("dpx:TimeCode", ""); int tmpint = m_spec.get_int_attribute ("dpx:TimeCode", ~0); if (tmpstr.size () > 0) m_dpx.header.SetTimeCode (tmpstr.c_str ()); else if (tmpint != ~0) m_dpx.header.timeCode = tmpint; m_dpx.header.userBits = m_spec.get_int_attribute ("dpx:UserBits", ~0); tmpstr = m_spec.get_string_attribute ("dpx:SourceDateTime", ""); if (tmpstr.size () >= 19) { // libdpx's date/time format is pretty close to OIIO's (libdpx uses // %Y:%m:%d:%H:%M:%S%Z) // NOTE: the following code relies on the DateTime attribute being properly // formatted! // assume UTC for simplicity's sake, fix it if someone complains tmpstr[10] = ':'; tmpstr.replace (19, -1, "Z"); m_dpx.header.SetSourceTimeDate (tmpstr.c_str ()); } // commit! if (!m_dpx.WriteHeader ()) { error ("Failed to write DPX header"); return false; } // user data ImageIOParameter *user = m_spec.find_attribute ("dpx:UserData"); if (user && user->datasize () > 0) { if (user->datasize () > 1024 * 1024) { error ("User data block size exceeds 1 MB"); return false; } // FIXME: write the missing libdpx code /*m_dpx.SetUserData (user->datasize ()); if (!m_dpx.WriteUserData ((void *)user->data ())) { error ("Failed to write user data"); return false; }*/ } // reserve space for the image data buffer m_buf.reserve (m_bytes * m_spec.height); return true; }
bool DPXOutput::prep_subimage (int s, bool allocate) { m_spec = m_subimage_specs[s]; // stash the spec // determine descriptor m_desc = get_image_descriptor(); // transfer function std::string colorspace = m_spec.get_string_attribute ("oiio:ColorSpace", ""); if (Strutil::iequals (colorspace, "Linear")) m_transfer = dpx::kLinear; else if (Strutil::iequals (colorspace, "GammaCorrected")) m_transfer = dpx::kUserDefined; else if (Strutil::iequals (colorspace, "Rec709")) m_transfer = dpx::kITUR709; else if (Strutil::iequals (colorspace, "KodakLog")) m_transfer = dpx::kLogarithmic; else { std::string dpxtransfer = m_spec.get_string_attribute ("dpx:Transfer", ""); m_transfer = get_characteristic_from_string (dpxtransfer); } // colorimetric m_cmetr = get_characteristic_from_string (m_spec.get_string_attribute ("dpx:Colorimetric", "User defined")); // select packing method std::string pck = m_spec.get_string_attribute ("dpx:Packing", "Filled, method A"); if (Strutil::iequals (pck, "Packed")) m_packing = dpx::kPacked; else if (Strutil::iequals (pck, "Filled, method B")) m_packing = dpx::kFilledMethodB; else m_packing = dpx::kFilledMethodA; switch (m_spec.format.basetype) { case TypeDesc::UINT8 : case TypeDesc::UINT16 : case TypeDesc::FLOAT : case TypeDesc::DOUBLE : // supported, fine break; case TypeDesc::HALF : // Turn half into float m_spec.format.basetype = TypeDesc::FLOAT; break; default: // Turn everything else into UINT16 m_spec.format.basetype = TypeDesc::UINT16; break; } // calculate target bit depth m_bitdepth = m_spec.format.size () * 8; if (m_spec.format == TypeDesc::UINT16) { m_bitdepth = m_spec.get_int_attribute ("oiio:BitsPerSample", 16); if (m_bitdepth != 10 && m_bitdepth != 12 && m_bitdepth != 16) { error ("Unsupported bit depth %d", m_bitdepth); return false; } } // Bug workaround: libDPX doesn't appear to correctly support // "filled method A" for 12 bit data. Does anybody care what // packing/filling we use? Punt and just use "packed". if (m_bitdepth == 12) m_packing = dpx::kPacked; if (m_spec.format == TypeDesc::UINT8 || m_spec.format == TypeDesc::INT8) m_datasize = dpx::kByte; else if (m_spec.format == TypeDesc::UINT16 || m_spec.format == TypeDesc::INT16) m_datasize = dpx::kWord; else if (m_spec.format == TypeDesc::FLOAT || m_spec.format == TypeDesc::HALF) { m_spec.format = TypeDesc::FLOAT; m_datasize = dpx::kFloat; } else if (m_spec.format == TypeDesc::DOUBLE) m_datasize = dpx::kDouble; else { // use 16-bit unsigned integers as a failsafe m_spec.set_format (TypeDesc::UINT16); m_datasize = dpx::kWord; } // check if the client is giving us raw data to write m_wantRaw = m_spec.get_int_attribute ("dpx:RawData", 0) != 0; // see if we'll need to convert or not if (m_desc == dpx::kRGB || m_desc == dpx::kRGBA) { // shortcut for RGB(A) that gets the job done m_bytes = m_spec.scanline_bytes (); m_wantRaw = true; } else { m_bytes = dpx::QueryNativeBufferSize (m_desc, m_datasize, m_spec.width, 1); if (m_bytes == 0 && !m_wantRaw) { error ("Unable to deliver native format data from source data"); return false; } else if (m_bytes < 0) { // no need to allocate another buffer if (!m_wantRaw) m_bytes = m_spec.scanline_bytes (); else m_bytes = -m_bytes; } } if (m_bytes < 0) m_bytes = -m_bytes; // allocate space for the image data buffer if (allocate) m_buf.resize (m_bytes * m_spec.height); return true; }