| 1 |
/* |
| 2 |
* Copyright 2023 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/codec/SkJpegMultiPicture.h" |
| 9 |
|
| 10 |
#include "include/core/SkData.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "src/base/SkEndian.h" |
| 13 |
#include "src/base/SkSafeMath.h" |
| 14 |
#include "src/codec/SkCodecPriv.h" |
| 15 |
#include "src/codec/SkJpegConstants.h" |
| 16 |
#include "src/codec/SkJpegSegmentScan.h" |
| 17 |
#include "src/codec/SkTiffUtility.h" |
| 18 |
#include "src/core/SkStreamPriv.h" |
| 19 |
|
| 20 |
#include <cstring> |
| 21 |
|
| 22 |
constexpr uint16_t kVersionTag = 0xB000; |
| 23 |
constexpr uint32_t kVersionCount = 4; |
| 24 |
constexpr size_t kVersionSize = 4; |
| 25 |
constexpr uint8_t kVersionExpected[kVersionSize] = {'0', '1', '0', '0'}; |
| 26 |
|
| 27 |
constexpr uint16_t kNumberOfImagesTag = 0xB001; |
| 28 |
constexpr uint32_t kNumberOfImagesCount = 1; |
| 29 |
|
| 30 |
constexpr uint16_t kMPEntryTag = 0xB002; |
| 31 |
constexpr uint32_t kMPEntrySize = 16; |
| 32 |
|
| 33 |
constexpr uint32_t kMPEntryAttributeFormatMask = 0x7000000; |
| 34 |
constexpr uint32_t kMPEntryAttributeFormatJpeg = 0x0000000; |
| 35 |
|
| 36 |
constexpr uint32_t kMPEntryAttributeTypeMask = 0xFFFFFF; |
| 37 |
constexpr uint32_t kMPEntryAttributeTypePrimary = 0x030000; |
| 38 |
|
| 39 |
constexpr uint16_t kIndividualImageUniqueIDTag = 0xB003; |
| 40 |
constexpr uint32_t kIndividualImageUniqueIDSize = 33; |
| 41 |
|
| 42 |
constexpr uint16_t kTotalNumberCapturedFramesTag = 0xB004; |
| 43 |
|
| 44 |
std::unique_ptr<SkJpegMultiPictureParameters> SkJpegMultiPictureParameters::Make( |
| 45 |
const sk_sp<const SkData>& segmentParameters) { |
| 46 |
// Read the MP Format identifier starting after the APP2 Field Length. See Figure 4 of CIPA |
| 47 |
// DC-x007-2009. |
| 48 |
if (segmentParameters->size() < sizeof(kMpfSig)) { |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
if (memcmp(segmentParameters->data(), kMpfSig, sizeof(kMpfSig)) != 0) { |
| 52 |
return nullptr; |
| 53 |
} |
| 54 |
auto ifdData = SkData::MakeSubset( |
| 55 |
segmentParameters.get(), sizeof(kMpfSig), segmentParameters->size() - sizeof(kMpfSig)); |
| 56 |
SkASSERT(ifdData); |
| 57 |
|
| 58 |
// The rest of this function reads the structure described in Figure 6 of CIPA DC-x007-2009. |
| 59 |
// Determine the endianness of the values in the structure. See Figure 5 (MP endian tag |
| 60 |
// structure), and read the Index IFD offset. |
| 61 |
bool littleEndian = false; |
| 62 |
uint32_t ifdOffset = 0; |
| 63 |
if (!SkTiff::ImageFileDirectory::ParseHeader(ifdData.get(), &littleEndian, &ifdOffset)) { |
| 64 |
SkCodecPrintf("Failed to parse endian-ness and index IFD offset.\n"); |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
// Create the Index Image File Directory (Index IFD). |
| 69 |
auto ifd = SkTiff::ImageFileDirectory::MakeFromOffset(ifdData, littleEndian, ifdOffset); |
| 70 |
if (!ifd) { |
| 71 |
SkCodecPrintf("Failed to create MP Index IFD offset.\n"); |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
|
| 75 |
// Read the number of tags in the Index IFD. See Table 3 (MP Index IFD Tags) for a description |
| 76 |
// of all possible tags. |
| 77 |
uint16_t tagCount = ifd->getNumEntries(); |
| 78 |
|
| 79 |
// We will extract the number of images from the tags. |
| 80 |
uint32_t numberOfImages = 0; |
| 81 |
|
| 82 |
// The data for the MP entries. |
| 83 |
sk_sp<SkData> mpEntriesData; |
| 84 |
|
| 85 |
// The MP Index IFD tags shall be specified in the order of their tag IDs (text from |
| 86 |
// section 5.2.3), so keep track of the previous tag id read. |
| 87 |
uint16_t previousTag = 0; |
| 88 |
for (uint16_t idfEntryIndex = 0; idfEntryIndex < tagCount; ++idfEntryIndex) { |
| 89 |
uint16_t tag = ifd->getEntryTag(idfEntryIndex); |
| 90 |
if (previousTag >= tag) { |
| 91 |
SkCodecPrintf("MPF tags not in order.\n"); |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
previousTag = tag; |
| 95 |
|
| 96 |
switch (tag) { |
| 97 |
case kVersionTag: { |
| 98 |
// See 5.2.3.1: MP Format Version. |
| 99 |
sk_sp<SkData> data = ifd->getEntryUndefinedData(idfEntryIndex); |
| 100 |
if (!data) { |
| 101 |
SkCodecPrintf("Version must be undefined type.\n"); |
| 102 |
return nullptr; |
| 103 |
} |
| 104 |
if (data->size() != kVersionSize) { |
| 105 |
SkCodecPrintf("Version must be 4 bytes.\n"); |
| 106 |
return nullptr; |
| 107 |
} |
| 108 |
if (memcmp(data->data(), kVersionExpected, kVersionSize) != 0) { |
| 109 |
SkCodecPrintf("Version value is not 0100.\n"); |
| 110 |
return nullptr; |
| 111 |
} |
| 112 |
break; |
| 113 |
} |
| 114 |
case kNumberOfImagesTag: |
| 115 |
// See 5.2.3.2: Number of Images. |
| 116 |
if (!ifd->getEntryUnsignedLong(idfEntryIndex, 1, &numberOfImages)) { |
| 117 |
SkCodecPrintf("Number of Images was not 1 unsigned long.\n"); |
| 118 |
} |
| 119 |
if (numberOfImages < 1) { |
| 120 |
SkCodecPrintf("Invalid number of images.\n"); |
| 121 |
return nullptr; |
| 122 |
} |
| 123 |
break; |
| 124 |
case kMPEntryTag: { |
| 125 |
// See 5.2.3.3: MP Entry. |
| 126 |
mpEntriesData = ifd->getEntryUndefinedData(idfEntryIndex); |
| 127 |
if (!mpEntriesData) { |
| 128 |
SkCodecPrintf("MP entries data could not be extracted.\n"); |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
SkSafeMath expectedSizeSafe; |
| 132 |
const size_t expectedSize = expectedSizeSafe.mul(kMPEntrySize, numberOfImages); |
| 133 |
if (!expectedSizeSafe.ok() || mpEntriesData->size() != expectedSize) { |
| 134 |
SkCodecPrintf("MP entries data should be %ux%u bytes, was %u.\n", |
| 135 |
kMPEntrySize, |
| 136 |
numberOfImages, |
| 137 |
static_cast<uint32_t>(mpEntriesData->size())); |
| 138 |
return nullptr; |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
case kIndividualImageUniqueIDTag: { |
| 143 |
// See 5.2.3.4: Individual Image Unique ID List. |
| 144 |
// Validate that the count parameter is correct, but do not extract any other |
| 145 |
// information. |
| 146 |
sk_sp<SkData> data = ifd->getEntryUndefinedData(idfEntryIndex); |
| 147 |
if (!data) { |
| 148 |
SkCodecPrintf("Image Unique ID must be undefined type.\n"); |
| 149 |
return nullptr; |
| 150 |
} |
| 151 |
if (data->size() != kIndividualImageUniqueIDSize * numberOfImages) { |
| 152 |
SkCodecPrintf("Invalid Image Unique ID count.\n"); |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
break; |
| 156 |
} |
| 157 |
case kTotalNumberCapturedFramesTag: { |
| 158 |
// See 5.2.3.5: Total Number of Captured Frames. |
| 159 |
uint32_t totalNumCapturedFrames = 0; |
| 160 |
if (!ifd->getEntryUnsignedLong(idfEntryIndex, 1, &totalNumCapturedFrames)) { |
| 161 |
SkCodecPrintf("Total Number of Captures Frames was not 1 unsigned long.\n"); |
| 162 |
} |
| 163 |
break; |
| 164 |
} |
| 165 |
default: |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
} |
| 169 |
if (!numberOfImages) { |
| 170 |
SkCodecPrintf("Number of images must be greater than zero.\n"); |
| 171 |
return nullptr; |
| 172 |
} |
| 173 |
if (!mpEntriesData) { |
| 174 |
SkCodecPrintf("MP Entry data was not present.\n"); |
| 175 |
return nullptr; |
| 176 |
} |
| 177 |
|
| 178 |
// Start to prepare the result that we will return. |
| 179 |
auto result = std::make_unique<SkJpegMultiPictureParameters>(numberOfImages); |
| 180 |
|
| 181 |
// The next IFD is the Attribute IFD offset. We will not read or validate the Attribute IFD. |
| 182 |
|
| 183 |
// Parse the MP Entries data. |
| 184 |
for (uint32_t i = 0; i < numberOfImages; ++i) { |
| 185 |
const auto mpEntry = mpEntriesData->shareSubset(kMPEntrySize * i, kMPEntrySize); |
| 186 |
if (!mpEntry) { |
| 187 |
SkCodecPrintf("MP Entry data unavailable\n"); |
| 188 |
return nullptr; |
| 189 |
} |
| 190 |
const uint32_t attribute = SkCodecPriv::GetEndianInt(mpEntry->bytes() + 0, littleEndian); |
| 191 |
const uint32_t size = SkCodecPriv::GetEndianInt(mpEntry->bytes() + 4, littleEndian); |
| 192 |
const uint32_t dataOffset = SkCodecPriv::GetEndianInt(mpEntry->bytes() + 8, littleEndian); |
| 193 |
|
| 194 |
const bool isPrimary = |
| 195 |
(attribute & kMPEntryAttributeTypeMask) == kMPEntryAttributeTypePrimary; |
| 196 |
const bool isJpeg = |
| 197 |
(attribute & kMPEntryAttributeFormatMask) == kMPEntryAttributeFormatJpeg; |
| 198 |
|
| 199 |
if (isPrimary != (i == 0)) { |
| 200 |
SkCodecPrintf("Image must be primary iff it is the first image..\n"); |
| 201 |
return nullptr; |
| 202 |
} |
| 203 |
if (!isJpeg) { |
| 204 |
SkCodecPrintf("Image format must be 0 (JPEG).\n"); |
| 205 |
return nullptr; |
| 206 |
} |
| 207 |
|
| 208 |
if (i == 0 && dataOffset != 0) { |
| 209 |
SkCodecPrintf("First individual Image offset must be NULL.\n"); |
| 210 |
return nullptr; |
| 211 |
} |
| 212 |
|
| 213 |
result->images[i].dataOffset = dataOffset; |
| 214 |
result->images[i].size = size; |
| 215 |
} |
| 216 |
|
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
sk_sp<SkData> SkJpegMultiPictureParameters::serialize(uint32_t individualImageNumber) const { |
| 221 |
SkDynamicMemoryWStream s; |
| 222 |
|
| 223 |
const uint32_t numberOfImages = static_cast<uint32_t>(images.size()); |
| 224 |
|
| 225 |
// Write the MPF signature. |
| 226 |
s.write(kMpfSig, sizeof(kMpfSig)); |
| 227 |
|
| 228 |
// We will always write as big-endian. |
| 229 |
s.write(SkTiff::kEndianBig, sizeof(SkTiff::kEndianBig)); |
| 230 |
|
| 231 |
// Set the first IFD offset be the position after the endianness value and this offset. This |
| 232 |
// will be the MP Index IFD for the first individual image and the MP Attribute IFD for all |
| 233 |
// other images. |
| 234 |
constexpr uint32_t firstIfdOffset = sizeof(SkTiff::kEndianBig) + // Endian-ness |
| 235 |
sizeof(uint32_t); // Index IFD offset |
| 236 |
SkStreamPriv::WriteU32BE(&s, firstIfdOffset); |
| 237 |
SkASSERT(s.bytesWritten() - sizeof(kMpfSig) == firstIfdOffset); |
| 238 |
|
| 239 |
if (individualImageNumber == 0) { |
| 240 |
// The MP Index IFD will write 3 tags (version, number of images, and MP entries). See |
| 241 |
// in Table 6 (MP Index IFD Tag Support Level) that these are the only mandatory entries. |
| 242 |
const uint32_t mpIndexIfdNumberOfTags = 3; |
| 243 |
SkStreamPriv::WriteU16BE(&s, mpIndexIfdNumberOfTags); |
| 244 |
} else { |
| 245 |
// The MP Attribute IFD will write 1 tags (version). See in Table 7 (MP Attribute IFD Tag |
| 246 |
// Support Level for Baseline MP Files) that no tags are required. If gainmap images support |
| 247 |
// is added to CIPA DC-007, then some tags may be added and become mandatory. |
| 248 |
const uint16_t mpAttributeIfdNumberOfTags = 1; |
| 249 |
SkStreamPriv::WriteU16BE(&s, mpAttributeIfdNumberOfTags); |
| 250 |
} |
| 251 |
|
| 252 |
// Write the version. |
| 253 |
SkStreamPriv::WriteU16BE(&s, kVersionTag); |
| 254 |
SkStreamPriv::WriteU16BE(&s, SkTiff::kTypeUndefined); |
| 255 |
SkStreamPriv::WriteU32BE(&s, kVersionCount); |
| 256 |
s.write(kVersionExpected, kVersionSize); |
| 257 |
|
| 258 |
if (individualImageNumber == 0) { |
| 259 |
// Write the number of images. |
| 260 |
SkStreamPriv::WriteU16BE(&s, kNumberOfImagesTag); |
| 261 |
SkStreamPriv::WriteU16BE(&s, SkTiff::kTypeUnsignedLong); |
| 262 |
SkStreamPriv::WriteU32BE(&s, kNumberOfImagesCount); |
| 263 |
SkStreamPriv::WriteU32BE(&s, numberOfImages); |
| 264 |
|
| 265 |
// Write the MP entries tag. |
| 266 |
SkStreamPriv::WriteU16BE(&s, kMPEntryTag); |
| 267 |
SkStreamPriv::WriteU16BE(&s, SkTiff::kTypeUndefined); |
| 268 |
const uint32_t mpEntriesSize = kMPEntrySize * numberOfImages; |
| 269 |
SkStreamPriv::WriteU32BE(&s, mpEntriesSize); |
| 270 |
const uint32_t mpEntryOffset = static_cast<uint32_t>( |
| 271 |
s.bytesWritten() - // The bytes written so far |
| 272 |
sizeof(kMpfSig) + // Excluding the MPF signature |
| 273 |
sizeof(uint32_t) + // The 4 bytes for this offset |
| 274 |
sizeof(uint32_t)); // The 4 bytes for the attribute IFD offset. |
| 275 |
SkStreamPriv::WriteU32BE(&s, mpEntryOffset); |
| 276 |
|
| 277 |
// Write the attribute IFD offset (zero because there is none). |
| 278 |
SkStreamPriv::WriteU32BE(&s, 0); |
| 279 |
|
| 280 |
// Write the MP entries data. |
| 281 |
SkASSERT(s.bytesWritten() - sizeof(kMpfSig) == mpEntryOffset); |
| 282 |
for (size_t i = 0; i < images.size(); ++i) { |
| 283 |
const auto& image = images[i]; |
| 284 |
|
| 285 |
uint32_t attribute = kMPEntryAttributeFormatJpeg; |
| 286 |
if (i == 0) { |
| 287 |
attribute |= kMPEntryAttributeTypePrimary; |
| 288 |
} |
| 289 |
|
| 290 |
SkStreamPriv::WriteU32BE(&s, attribute); |
| 291 |
SkStreamPriv::WriteU32BE(&s, image.size); |
| 292 |
SkStreamPriv::WriteU32BE(&s, image.dataOffset); |
| 293 |
// Dependent image 1 and 2 entries are zero. |
| 294 |
SkStreamPriv::WriteU16BE(&s, 0); |
| 295 |
SkStreamPriv::WriteU16BE(&s, 0); |
| 296 |
} |
| 297 |
} else { |
| 298 |
// The non-first-individual-images do not have any further IFDs. |
| 299 |
SkStreamPriv::WriteU32BE(&s, 0); |
| 300 |
} |
| 301 |
|
| 302 |
return s.detachAsData(); |
| 303 |
} |
| 304 |
|
| 305 |
static size_t mp_header_absolute_offset(size_t mpSegmentOffset) { |
| 306 |
return mpSegmentOffset + // The offset to the segment's marker |
| 307 |
kJpegMarkerCodeSize + // The marker itself |
| 308 |
kJpegSegmentParameterLengthSize + // The segment parameter length |
| 309 |
sizeof(kMpfSig); // The {'M','P','F',0} signature |
| 310 |
} |
| 311 |
|
| 312 |
size_t SkJpegMultiPictureParameters::GetImageAbsoluteOffset(uint32_t dataOffset, |
| 313 |
size_t mpSegmentOffset) { |
| 314 |
// The value of zero is used by the primary image. |
| 315 |
if (dataOffset == 0) { |
| 316 |
return 0; |
| 317 |
} |
| 318 |
return mp_header_absolute_offset(mpSegmentOffset) + dataOffset; |
| 319 |
} |
| 320 |
|
| 321 |
uint32_t SkJpegMultiPictureParameters::GetImageDataOffset(size_t imageAbsoluteOffset, |
| 322 |
size_t mpSegmentOffset) { |
| 323 |
// The value of zero is used by the primary image. |
| 324 |
if (imageAbsoluteOffset == 0) { |
| 325 |
return 0; |
| 326 |
} |
| 327 |
return static_cast<uint32_t>(imageAbsoluteOffset - mp_header_absolute_offset(mpSegmentOffset)); |
| 328 |
} |