-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2153 from Exiv2/027_FixJp2
0.27 - Fix JP2 write/read metadata
- Loading branch information
Showing
11 changed files
with
608 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "jp2image_int.hpp" | ||
|
||
#include <cassert> | ||
|
||
#include "error.hpp" | ||
#include "types.hpp" | ||
|
||
namespace Exiv2 { | ||
namespace Internal { | ||
bool isValidBoxFileType(const std::vector<uint8_t> &boxData) | ||
{ | ||
// BR & MinV are obligatory (4 + 4 bytes). Afterwards we have N compatibility | ||
// lists (of size 4) | ||
if ((boxData.size() - 8u) % 4u != 0) { | ||
return false; | ||
} | ||
|
||
const size_t N = (boxData.size() - 8u) / 4u; | ||
const uint32_t brand = getULong(boxData.data(), bigEndian); | ||
const uint32_t minorVersion = getULong(boxData.data() + 4, bigEndian); | ||
|
||
bool clWithRightBrand = false; | ||
for (size_t i = 0; i < N; i++) { | ||
uint32_t compatibilityList = getULong(boxData.data() + 8 + i * 4, bigEndian); | ||
if (compatibilityList == brandJp2) { | ||
clWithRightBrand = true; | ||
break; | ||
} | ||
} | ||
return (brand == brandJp2 && minorVersion == 0 && clWithRightBrand); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef JP2IMAGE_INT_HPP | ||
#define JP2IMAGE_INT_HPP | ||
|
||
#include <stdint.h> | ||
#include <vector> | ||
|
||
namespace Exiv2 | ||
{ | ||
namespace Internal | ||
{ | ||
struct Jp2BoxHeader | ||
{ | ||
uint32_t length; | ||
uint32_t type; | ||
}; | ||
|
||
struct Jp2ImageHeaderBox | ||
{ | ||
uint32_t imageHeight; | ||
uint32_t imageWidth; | ||
uint16_t componentCount; | ||
uint8_t bpc; //<! Bits per component | ||
uint8_t c; //<! Compression type | ||
uint8_t unkC; //<! Colourspace unknown | ||
uint8_t ipr; //<! Intellectual property | ||
}; | ||
|
||
struct Jp2UuidBox | ||
{ | ||
uint8_t uuid[16]; | ||
}; | ||
|
||
const uint32_t brandJp2 = 0x6a703220; | ||
|
||
/// @brief Determines if the File Type box is valid | ||
bool isValidBoxFileType(const std::vector<uint8_t>& boxData); | ||
} // namespace Internal | ||
} // namespace Exiv2 | ||
|
||
#endif // JP2IMAGE_INT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include <exiv2/jp2image.hpp> | ||
#include <exiv2/error.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace Exiv2; | ||
|
||
TEST(Jp2Image, canBeCreatedFromScratch) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
ASSERT_NO_THROW(newJp2Instance(memIo, create)); | ||
} | ||
|
||
TEST(Jp2Image, canBeOpenedEvenWithAnEmptyMemIo) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = false; | ||
ASSERT_NO_THROW(newJp2Instance(memIo, create)); | ||
} | ||
|
||
TEST(Jp2Image, mimeTypeIsPng) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
ASSERT_EQ("image/jp2", image->mimeType()); | ||
} | ||
|
||
TEST(Jp2Image, printStructurePrintsNothingWithKpsNone) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
|
||
std::ostringstream stream; | ||
image->printStructure(stream, Exiv2::kpsNone, 1); | ||
|
||
ASSERT_TRUE(stream.str().empty()); | ||
} | ||
|
||
TEST(Jp2Image, printStructurePrintsDataWithKpsBasic) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
|
||
std::ostringstream stream; | ||
image->printStructure(stream, Exiv2::kpsBasic, 1); | ||
|
||
ASSERT_FALSE(stream.str().empty()); | ||
} | ||
|
||
TEST(Jp2Image, cannotReadMetadataFromEmptyIo) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = false; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
ASSERT_TRUE(image.get() == NULL); | ||
} | ||
|
||
TEST(Jp2Image, cannotReadMetadataFromIoWhichCannotBeOpened) | ||
{ | ||
Exiv2::BasicIo::AutoPtr io (new Exiv2::FileIo("NonExistingPath.jp2")); | ||
const bool create = false; | ||
Image::AutoPtr image = newJp2Instance(io, create); | ||
ASSERT_TRUE(image.get() == NULL); | ||
} | ||
|
||
TEST(Jp2Image, cannotWriteMetadataToEmptyIo) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = false; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
ASSERT_TRUE(image.get() == NULL); | ||
} | ||
|
||
TEST(Jp2Image, canWriteMetadataFromCreatedJp2Image) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
ASSERT_NO_THROW(image->writeMetadata()); | ||
} | ||
|
||
TEST(Jp2Image, canWriteMetadataAndReadAfterwards) | ||
{ | ||
Exiv2::BasicIo::AutoPtr memIo (new Exiv2::MemIo); | ||
const bool create = true; | ||
Image::AutoPtr image = newJp2Instance(memIo, create); | ||
|
||
ASSERT_NO_THROW(image->writeMetadata()); | ||
ASSERT_NO_THROW(image->readMetadata()); | ||
} |