From 1a0d26aff8b8e063cc7a084c4408a65e572dd4ec Mon Sep 17 00:00:00 2001 From: Shuning Bian Date: Fri, 23 May 2025 23:55:50 +1000 Subject: [PATCH] Adds an option to specify the predictor to use when compression is enabled and adjusts the jpeg... image size accordingly. This set of changes allows us to generate darktable compatible compressed DNGs because darktable _only_ supports predictor=1 which requires the JPEG image size to be exactly the DNG image size. --- src/pidng/core.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/pidng/core.py b/src/pidng/core.py index 5ec8d00..d1f07c2 100644 --- a/src/pidng/core.py +++ b/src/pidng/core.py @@ -45,7 +45,7 @@ def __filter__(self, rawFrame: np.ndarray, filter : types.FunctionType) -> np.nd return processed - def __process__(self, rawFrame : np.ndarray, tags: DNGTags, compress : bool) -> bytearray: + def __process__(self, rawFrame : np.ndarray, tags: DNGTags, compress : bool, predictor: int = 6) -> bytearray: width = tags.get(Tag.ImageWidth).rawValue[0] length = tags.get(Tag.ImageLength).rawValue[0] @@ -61,12 +61,20 @@ def __process__(self, rawFrame : np.ndarray, tags: DNGTags, compress : bool) -> backward_version = DNGVersion.V1_4 # Floating-point data has to be compressed with deflate if compress: - raise Exception('Compression is not supported for floating-point data') + raise Exception("Compression is not supported for floating-point data") if compress: from ljpegCompress import pack16tolj - tile = pack16tolj(rawFrame, int(width*2), - int(length/2), bpp, 0, 0, 0, "", 6) + + if predictor == 1: + tile = pack16tolj(rawFrame, int(width), + int(length), bpp, 0, 0, 0, "", 1) + elif predictor == 6: + tile = pack16tolj(rawFrame, int(width*2), + int(length/2), bpp, 0, 0, 0, "", 6) + else: + raise Exception("Predictor must be either 1 or 6") + else: if bpp == 8: tile = rawFrame.astype('uint8').tobytes() @@ -116,11 +124,12 @@ def __process__(self, rawFrame : np.ndarray, tags: DNGTags, compress : bool) -> return buf - def options(self, tags : DNGTags, path : str, compress=False) -> None: + def options(self, tags : DNGTags, path : str, compress=False, predictor=6) -> None: self.__tags_condition__(tags) self.tags = tags self.compress = compress self.path = path + self.predictor = predictor def convert(self, image : np.ndarray, filename=""): @@ -131,7 +140,7 @@ def convert(self, image : np.ndarray, filename=""): self.__data_condition__(image) unpacked = self.__unpack_pixels__(image) filtered = self.__filter__(unpacked, self.filter) - buf = self.__process__(filtered, self.tags, self.compress) + buf = self.__process__(filtered, self.tags, self.compress, self.predictor) file_output = False if len(filename) > 0: