Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "TiffWriter" |
| 18 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 19 | #include <img_utils/TiffHelpers.h> |
| 20 | #include <img_utils/TiffWriter.h> |
| 21 | #include <img_utils/TagDefinitions.h> |
| 22 | |
| 23 | #include <assert.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace img_utils { |
| 27 | |
| 28 | KeyedVector<uint16_t, const TagDefinition_t*> TiffWriter::buildTagMap( |
| 29 | const TagDefinition_t* definitions, size_t length) { |
| 30 | KeyedVector<uint16_t, const TagDefinition_t*> map; |
| 31 | for(size_t i = 0; i < length; ++i) { |
| 32 | map.add(definitions[i].tagId, definitions + i); |
| 33 | } |
| 34 | return map; |
| 35 | } |
| 36 | |
| 37 | #define COMPARE(op) \ |
| 38 | bool Orderable::operator op (const Orderable& orderable) const { \ |
| 39 | return getComparableValue() op orderable.getComparableValue(); \ |
| 40 | } |
| 41 | |
| 42 | #define ARRAY_SIZE(array) \ |
| 43 | (sizeof(array) / sizeof(array[0])) |
| 44 | |
| 45 | KeyedVector<uint16_t, const TagDefinition_t*> TiffWriter::sTagMaps[] = { |
| 46 | buildTagMap(TIFF_EP_TAG_DEFINITIONS, ARRAY_SIZE(TIFF_EP_TAG_DEFINITIONS)), |
| 47 | buildTagMap(DNG_TAG_DEFINITIONS, ARRAY_SIZE(DNG_TAG_DEFINITIONS)), |
| 48 | buildTagMap(EXIF_2_3_TAG_DEFINITIONS, ARRAY_SIZE(EXIF_2_3_TAG_DEFINITIONS)), |
| 49 | buildTagMap(TIFF_6_TAG_DEFINITIONS, ARRAY_SIZE(TIFF_6_TAG_DEFINITIONS)) |
| 50 | }; |
| 51 | |
| 52 | TiffWriter::TiffWriter() : mTagMaps(sTagMaps), mNumTagMaps(DEFAULT_NUM_TAG_MAPS) {} |
| 53 | |
| 54 | TiffWriter::TiffWriter(KeyedVector<uint16_t, const TagDefinition_t*>* enabledDefinitions, |
| 55 | size_t length) : mTagMaps(enabledDefinitions), mNumTagMaps(length) {} |
| 56 | |
| 57 | TiffWriter::~TiffWriter() {} |
| 58 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 59 | status_t TiffWriter::write(Output* out, StripSource** sources, size_t sourcesCount, |
| 60 | Endianness end) { |
| 61 | status_t ret = OK; |
| 62 | EndianOutput endOut(out, end); |
| 63 | |
| 64 | if (mIfd == NULL) { |
| 65 | ALOGE("%s: Tiff header is empty.", __FUNCTION__); |
| 66 | return BAD_VALUE; |
| 67 | } |
| 68 | |
| 69 | if (LOG_NDEBUG == 0) { |
| 70 | log(); |
| 71 | } |
| 72 | |
| 73 | uint32_t totalSize = getTotalSize(); |
| 74 | |
| 75 | KeyedVector<uint32_t, uint32_t> offsetVector; |
| 76 | |
| 77 | for (size_t i = 0; i < mNamedIfds.size(); ++i) { |
| 78 | if (mNamedIfds[i]->uninitializedOffsets()) { |
| 79 | uint32_t stripSize = mNamedIfds[i]->getStripSize(); |
| 80 | if (mNamedIfds[i]->setStripOffset(totalSize) != OK) { |
| 81 | ALOGE("%s: Could not set strip offsets.", __FUNCTION__); |
| 82 | return BAD_VALUE; |
| 83 | } |
| 84 | totalSize += stripSize; |
| 85 | WORD_ALIGN(totalSize); |
| 86 | offsetVector.add(mNamedIfds.keyAt(i), totalSize); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | size_t offVecSize = offsetVector.size(); |
| 91 | if (offVecSize != sourcesCount) { |
| 92 | ALOGE("%s: Mismatch between number of IFDs with uninitialized strips (%zu) and" |
| 93 | " sources (%zu).", __FUNCTION__, offVecSize, sourcesCount); |
| 94 | return BAD_VALUE; |
| 95 | } |
| 96 | |
| 97 | BAIL_ON_FAIL(writeFileHeader(endOut), ret); |
| 98 | |
| 99 | uint32_t offset = FILE_HEADER_SIZE; |
| 100 | sp<TiffIfd> ifd = mIfd; |
| 101 | while(ifd != NULL) { |
| 102 | BAIL_ON_FAIL(ifd->writeData(offset, &endOut), ret); |
| 103 | offset += ifd->getSize(); |
| 104 | ifd = ifd->getNextIfd(); |
| 105 | } |
| 106 | |
| 107 | log(); |
| 108 | |
| 109 | for (size_t i = 0; i < offVecSize; ++i) { |
| 110 | uint32_t ifdKey = offsetVector.keyAt(i); |
| 111 | uint32_t nextOffset = offsetVector[i]; |
| 112 | uint32_t sizeToWrite = mNamedIfds[ifdKey]->getStripSize(); |
| 113 | bool found = false; |
| 114 | for (size_t j = 0; j < sourcesCount; ++j) { |
| 115 | if (sources[j]->getIfd() == ifdKey) { |
| 116 | if ((ret = sources[i]->writeToStream(endOut, sizeToWrite)) != OK) { |
| 117 | ALOGE("%s: Could not write to stream, received %d.", __FUNCTION__, ret); |
| 118 | return ret; |
| 119 | } |
| 120 | ZERO_TILL_WORD(&endOut, sizeToWrite, ret); |
| 121 | found = true; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | if (!found) { |
| 126 | ALOGE("%s: No stream for byte strips for IFD %u", __FUNCTION__, ifdKey); |
| 127 | return BAD_VALUE; |
| 128 | } |
| 129 | assert(nextOffset == endOut.getCurrentOffset()); |
| 130 | } |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 135 | status_t TiffWriter::write(Output* out, Endianness end) { |
| 136 | status_t ret = OK; |
| 137 | EndianOutput endOut(out, end); |
| 138 | |
| 139 | if (mIfd == NULL) { |
| 140 | ALOGE("%s: Tiff header is empty.", __FUNCTION__); |
| 141 | return BAD_VALUE; |
| 142 | } |
| 143 | BAIL_ON_FAIL(writeFileHeader(endOut), ret); |
| 144 | |
| 145 | uint32_t offset = FILE_HEADER_SIZE; |
| 146 | sp<TiffIfd> ifd = mIfd; |
| 147 | while(ifd != NULL) { |
| 148 | BAIL_ON_FAIL(ifd->writeData(offset, &endOut), ret); |
| 149 | offset += ifd->getSize(); |
| 150 | ifd = ifd->getNextIfd(); |
| 151 | } |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | const TagDefinition_t* TiffWriter::lookupDefinition(uint16_t tag) const { |
| 157 | const TagDefinition_t* definition = NULL; |
| 158 | for (size_t i = 0; i < mNumTagMaps; ++i) { |
| 159 | ssize_t index = mTagMaps[i].indexOfKey(tag); |
| 160 | if (index >= 0) { |
| 161 | definition = mTagMaps[i][index]; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (definition == NULL) { |
| 167 | ALOGE("%s: No definition exists for tag with id %x.", __FUNCTION__, tag); |
| 168 | } |
| 169 | return definition; |
| 170 | } |
| 171 | |
| 172 | sp<TiffEntry> TiffWriter::getEntry(uint16_t tag, uint32_t ifd) const { |
| 173 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 174 | if (index < 0) { |
| 175 | ALOGE("%s: No IFD %d set for this writer.", __FUNCTION__, ifd); |
| 176 | return NULL; |
| 177 | } |
| 178 | return mNamedIfds[index]->getEntry(tag); |
| 179 | } |
| 180 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 181 | void TiffWriter::removeEntry(uint16_t tag, uint32_t ifd) { |
| 182 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 183 | if (index >= 0) { |
| 184 | mNamedIfds[index]->removeEntry(tag); |
| 185 | } |
| 186 | } |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 187 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 188 | status_t TiffWriter::addEntry(const sp<TiffEntry>& entry, uint32_t ifd) { |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 189 | uint16_t tag = entry->getTag(); |
| 190 | |
| 191 | const TagDefinition_t* definition = lookupDefinition(tag); |
| 192 | |
| 193 | if (definition == NULL) { |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 194 | ALOGE("%s: No definition exists for tag 0x%x.", __FUNCTION__, tag); |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 195 | return BAD_INDEX; |
| 196 | } |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 197 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 198 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 199 | |
| 200 | // Add a new IFD if necessary |
| 201 | if (index < 0) { |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 202 | ALOGE("%s: No IFD %u exists.", __FUNCTION__, ifd); |
| 203 | return NAME_NOT_FOUND; |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | sp<TiffIfd> selectedIfd = mNamedIfds[index]; |
| 207 | return selectedIfd->addEntry(entry); |
| 208 | } |
| 209 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 210 | status_t TiffWriter::addStrip(uint32_t ifd) { |
| 211 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 212 | if (index < 0) { |
| 213 | ALOGE("%s: Ifd %u doesn't exist, cannot add strip entries.", __FUNCTION__, ifd); |
| 214 | return BAD_VALUE; |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 215 | } |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 216 | sp<TiffIfd> selected = mNamedIfds[index]; |
| 217 | return selected->validateAndSetStripTags(); |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | status_t TiffWriter::addIfd(uint32_t ifd) { |
| 221 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 222 | if (index >= 0) { |
| 223 | ALOGE("%s: Ifd with ID 0x%x already exists.", __FUNCTION__, ifd); |
| 224 | return BAD_VALUE; |
| 225 | } |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 226 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 227 | sp<TiffIfd> newIfd = new TiffIfd(ifd); |
| 228 | if (mIfd == NULL) { |
| 229 | mIfd = newIfd; |
| 230 | } else { |
| 231 | sp<TiffIfd> last = findLastIfd(); |
| 232 | last->setNextIfd(newIfd); |
| 233 | } |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 234 | |
| 235 | if(mNamedIfds.add(ifd, newIfd) < 0) { |
| 236 | ALOGE("%s: Failed to add new IFD 0x%x.", __FUNCTION__, ifd); |
| 237 | return BAD_VALUE; |
| 238 | } |
| 239 | |
| 240 | return OK; |
| 241 | } |
| 242 | |
| 243 | status_t TiffWriter::addSubIfd(uint32_t parentIfd, uint32_t ifd, SubIfdType type) { |
| 244 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 245 | if (index >= 0) { |
| 246 | ALOGE("%s: Ifd with ID 0x%x already exists.", __FUNCTION__, ifd); |
| 247 | return BAD_VALUE; |
| 248 | } |
| 249 | |
| 250 | ssize_t parentIndex = mNamedIfds.indexOfKey(parentIfd); |
| 251 | if (parentIndex < 0) { |
| 252 | ALOGE("%s: Parent IFD with ID 0x%x does not exist.", __FUNCTION__, parentIfd); |
| 253 | return BAD_VALUE; |
| 254 | } |
| 255 | |
| 256 | sp<TiffIfd> parent = mNamedIfds[parentIndex]; |
| 257 | sp<TiffIfd> newIfd = new TiffIfd(ifd); |
| 258 | |
| 259 | uint16_t subIfdTag; |
| 260 | if (type == SUBIFD) { |
| 261 | subIfdTag = TAG_SUBIFDS; |
| 262 | } else if (type == GPSINFO) { |
| 263 | subIfdTag = TAG_GPSINFO; |
| 264 | } else { |
| 265 | ALOGE("%s: Unknown SubIFD type %d.", __FUNCTION__, type); |
| 266 | return BAD_VALUE; |
| 267 | } |
| 268 | |
| 269 | sp<TiffEntry> subIfds = parent->getEntry(subIfdTag); |
| 270 | if (subIfds == NULL) { |
| 271 | if (buildEntry(subIfdTag, 1, &newIfd, &subIfds) < 0) { |
| 272 | ALOGE("%s: Failed to build SubIfd entry in IFD 0x%x.", __FUNCTION__, parentIfd); |
| 273 | return BAD_VALUE; |
| 274 | } |
| 275 | } else { |
| 276 | if (type == GPSINFO) { |
| 277 | ALOGE("%s: Cannot add GPSInfo SubIFD to IFD %u, one already exists.", __FUNCTION__, |
| 278 | ifd); |
| 279 | return BAD_VALUE; |
| 280 | } |
| 281 | |
| 282 | Vector<sp<TiffIfd> > subIfdList; |
| 283 | const sp<TiffIfd>* oldIfdArray = subIfds->getData<sp<TiffIfd> >(); |
| 284 | if (subIfdList.appendArray(oldIfdArray, subIfds->getCount()) < 0) { |
| 285 | ALOGE("%s: Failed to build SubIfd entry in IFD 0x%x.", __FUNCTION__, parentIfd); |
| 286 | return BAD_VALUE; |
| 287 | } |
| 288 | |
| 289 | if (subIfdList.add(newIfd) < 0) { |
| 290 | ALOGE("%s: Failed to build SubIfd entry in IFD 0x%x.", __FUNCTION__, parentIfd); |
| 291 | return BAD_VALUE; |
| 292 | } |
| 293 | |
| 294 | uint32_t count = subIfdList.size(); |
| 295 | if (buildEntry(subIfdTag, count, subIfdList.array(), &subIfds) < 0) { |
| 296 | ALOGE("%s: Failed to build SubIfd entry in IFD 0x%x.", __FUNCTION__, parentIfd); |
| 297 | return BAD_VALUE; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (parent->addEntry(subIfds) < 0) { |
| 302 | ALOGE("%s: Failed to add SubIfd entry in IFD 0x%x.", __FUNCTION__, parentIfd); |
| 303 | return BAD_VALUE; |
| 304 | } |
| 305 | |
| 306 | if(mNamedIfds.add(ifd, newIfd) < 0) { |
| 307 | ALOGE("%s: Failed to add new IFD 0x%x.", __FUNCTION__, ifd); |
| 308 | return BAD_VALUE; |
| 309 | } |
| 310 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 311 | return OK; |
| 312 | } |
| 313 | |
| 314 | TagType TiffWriter::getDefaultType(uint16_t tag) const { |
| 315 | const TagDefinition_t* definition = lookupDefinition(tag); |
| 316 | if (definition == NULL) { |
| 317 | ALOGE("%s: Could not find definition for tag %x", __FUNCTION__, tag); |
| 318 | return UNKNOWN_TAGTYPE; |
| 319 | } |
| 320 | return definition->defaultType; |
| 321 | } |
| 322 | |
| 323 | uint32_t TiffWriter::getDefaultCount(uint16_t tag) const { |
| 324 | const TagDefinition_t* definition = lookupDefinition(tag); |
| 325 | if (definition == NULL) { |
| 326 | ALOGE("%s: Could not find definition for tag %x", __FUNCTION__, tag); |
| 327 | return 0; |
| 328 | } |
| 329 | return definition->fixedCount; |
| 330 | } |
| 331 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 332 | bool TiffWriter::hasIfd(uint32_t ifd) const { |
| 333 | ssize_t index = mNamedIfds.indexOfKey(ifd); |
| 334 | return index >= 0; |
| 335 | } |
| 336 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 337 | bool TiffWriter::checkIfDefined(uint16_t tag) const { |
| 338 | return lookupDefinition(tag) != NULL; |
| 339 | } |
| 340 | |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 341 | const char* TiffWriter::getTagName(uint16_t tag) const { |
| 342 | const TagDefinition_t* definition = lookupDefinition(tag); |
| 343 | if (definition == NULL) { |
| 344 | return NULL; |
| 345 | } |
| 346 | return definition->tagName; |
| 347 | } |
| 348 | |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 349 | sp<TiffIfd> TiffWriter::findLastIfd() { |
| 350 | sp<TiffIfd> ifd = mIfd; |
| 351 | while(ifd != NULL) { |
| 352 | sp<TiffIfd> nextIfd = ifd->getNextIfd(); |
| 353 | if (nextIfd == NULL) { |
| 354 | break; |
| 355 | } |
| 356 | ifd = nextIfd; |
| 357 | } |
| 358 | return ifd; |
| 359 | } |
| 360 | |
| 361 | status_t TiffWriter::writeFileHeader(EndianOutput& out) { |
| 362 | status_t ret = OK; |
| 363 | uint16_t endMarker = (out.getEndianness() == BIG) ? BIG_ENDIAN_MARKER : LITTLE_ENDIAN_MARKER; |
| 364 | BAIL_ON_FAIL(out.write(&endMarker, 0, 1), ret); |
| 365 | |
| 366 | uint16_t tiffMarker = TIFF_FILE_MARKER; |
| 367 | BAIL_ON_FAIL(out.write(&tiffMarker, 0, 1), ret); |
| 368 | |
| 369 | uint32_t offsetMarker = FILE_HEADER_SIZE; |
| 370 | BAIL_ON_FAIL(out.write(&offsetMarker, 0, 1), ret); |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | uint32_t TiffWriter::getTotalSize() const { |
| 375 | uint32_t totalSize = FILE_HEADER_SIZE; |
| 376 | sp<TiffIfd> ifd = mIfd; |
| 377 | while(ifd != NULL) { |
| 378 | totalSize += ifd->getSize(); |
| 379 | ifd = ifd->getNextIfd(); |
| 380 | } |
| 381 | return totalSize; |
| 382 | } |
| 383 | |
| 384 | void TiffWriter::log() const { |
| 385 | ALOGI("%s: TiffWriter:", __FUNCTION__); |
Ruben Brunk | 4510de2 | 2014-05-28 18:42:37 -0700 | [diff] [blame^] | 386 | size_t length = mNamedIfds.size(); |
| 387 | for (size_t i = 0; i < length; ++i) { |
| 388 | mNamedIfds[i]->log(); |
Ruben Brunk | e507721 | 2014-04-28 16:39:12 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | } /*namespace img_utils*/ |
| 393 | } /*namespace android*/ |