Commit 01074177 authored by Davis King's avatar Davis King

Upgraded the dng image format so it can natively store floating point

pixel types without any information loss.
parent 90668383
This diff is collapsed.
......@@ -20,7 +20,8 @@ namespace dlib
rgb_paeth,
rgb_alpha,
rgb_alpha_paeth,
grayscale_16bit
grayscale_16bit,
grayscale_float
};
const unsigned long dng_magic_byte = 100;
......
......@@ -15,6 +15,8 @@
#include "dng_shared.h"
#include "../uintn.h"
#include "../dir_nav.h"
#include "../float_details.h"
#include "../vectorstream.h"
namespace dlib
{
......@@ -249,7 +251,9 @@ namespace dlib
false,
pixel_traits<typename image_type::type>::rgb_alpha,
false,
pixel_traits<typename image_type::type>::grayscale && sizeof(typename image_type::type) != 1
pixel_traits<typename image_type::type>::grayscale && sizeof(typename image_type::type) != 1 &&
!is_float_type<typename image_type::type>::value,
is_float_type<typename image_type::type>::value
>::value
>
struct save_dng_helper;
......@@ -257,6 +261,64 @@ namespace dlib
typedef entropy_encoder::kernel_2a encoder_type;
typedef entropy_encoder_model<256,encoder_type>::kernel_5a eem_type;
typedef entropy_encoder_model<256,encoder_type>::kernel_4a eem_exp_type;
template <typename image_type >
struct save_dng_helper<image_type, grayscale_float>
{
static void save_dng (
const image_type& image,
std::ostream& out
)
{
out.write("DNG",3);
unsigned long version = 1;
serialize(version,out);
unsigned long type = grayscale_float;
serialize(type,out);
serialize(image.nc(),out);
serialize(image.nr(),out);
// Write the compressed exponent data into expbuf. We will append it
// to the stream at the end of the loops.
std::vector<char> expbuf;
expbuf.reserve(image.size()*2);
vectorstream outexp(expbuf);
encoder_type encoder;
encoder.set_stream(outexp);
eem_exp_type eem_exp(encoder);
float_details prev;
for (long r = 0; r < image.nr(); ++r)
{
for (long c = 0; c < image.nc(); ++c)
{
float_details cur = image[r][c];
int16 exp = cur.exponent-prev.exponent;
int64 man = cur.mantissa-prev.mantissa;
prev = cur;
unsigned char ebyte1 = exp&0xFF;
unsigned char ebyte2 = exp>>8;
eem_exp.encode(ebyte1);
eem_exp.encode(ebyte2);
serialize(man, out);
}
}
// write out the magic byte to mark the end of the compressed data.
eem_exp.encode(dng_magic_byte);
eem_exp.encode(dng_magic_byte);
eem_exp.encode(dng_magic_byte);
eem_exp.encode(dng_magic_byte);
encoder.clear();
serialize(expbuf, out);
}
};
template <typename image_type >
struct save_dng_helper<image_type, grayscale_16bit>
{
......
......@@ -90,11 +90,11 @@ namespace dlib
- image[0][0] will be in the upper left corner of the image.
- image[image.nr()-1][image.nc()-1] will be in the lower right
corner of the image.
- This routine can save images containing any type of pixel. However, the
DNG format can natively store only the following pixel types: rgb_pixel,
hsi_pixel, rgb_alpha_pixel, uint8, and uint16. All other pixel types
will be converted into one of these types as appropriate before being
saved to disk.
- This routine can save images containing any type of pixel. However, the DNG
format can natively store only the following pixel types: rgb_pixel,
hsi_pixel, rgb_alpha_pixel, uint8, uint16, float, double, and long double.
All other pixel types will be converted into one of these types as
appropriate before being saved to disk.
throws
- image_save_error
This exception is thrown if there is an error that prevents us
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment