1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
|
//
// TinyDNGWriter, single header only DNG writer in C++11.
//
/*
The MIT License (MIT)
Copyright (c) 2016 - 2020 Syoyo Fujita.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef TINY_DNG_WRITER_H_
#define TINY_DNG_WRITER_H_
#include <sstream>
#include <vector>
namespace tinydngwriter {
typedef enum {
TIFFTAG_SUB_FILETYPE = 254,
TIFFTAG_IMAGE_WIDTH = 256,
TIFFTAG_IMAGE_LENGTH = 257,
TIFFTAG_BITS_PER_SAMPLE = 258,
TIFFTAG_COMPRESSION = 259,
TIFFTAG_PHOTOMETRIC = 262,
TIFFTAG_IMAGEDESCRIPTION = 270,
TIFFTAG_STRIP_OFFSET = 273,
TIFFTAG_SAMPLES_PER_PIXEL = 277,
TIFFTAG_ROWS_PER_STRIP = 278,
TIFFTAG_STRIP_BYTE_COUNTS = 279,
TIFFTAG_PLANAR_CONFIG = 284,
TIFFTAG_ORIENTATION = 274,
TIFFTAG_XRESOLUTION = 282, // rational
TIFFTAG_YRESOLUTION = 283, // rational
TIFFTAG_RESOLUTION_UNIT = 296,
TIFFTAG_SAMPLEFORMAT = 339,
// DNG extension
TIFFTAG_CFA_REPEAT_PATTERN_DIM = 33421,
TIFFTAG_CFA_PATTERN = 33422,
TIFFTAG_DNG_VERSION = 50706,
TIFFTAG_DNG_BACKWARD_VERSION = 50707,
TIFFTAG_CHRROMA_BLUR_RADIUS = 50703,
TIFFTAG_BLACK_LEVEL = 50714,
TIFFTAG_WHITE_LEVEL = 50717,
TIFFTAG_COLOR_MATRIX1 = 50721,
TIFFTAG_COLOR_MATRIX2 = 50722,
TIFFTAG_EXTRA_CAMERA_PROFILES = 50933,
TIFFTAG_PROFILE_NAME = 50936,
TIFFTAG_AS_SHOT_PROFILE_NAME = 50934,
TIFFTAG_DEFAULT_BLACK_RENDER = 51110,
TIFFTAG_ACTIVE_AREA = 50829,
TIFFTAG_FORWARD_MATRIX1 = 50964,
TIFFTAG_FORWARD_MATRIX2 = 50965
} Tag;
// SUBFILETYPE(bit field)
static const int FILETYPE_REDUCEDIMAGE = 1;
static const int FILETYPE_PAGE = 2;
static const int FILETYPE_MASK = 4;
// PLANARCONFIG
static const int PLANARCONFIG_CONTIG = 1;
static const int PLANARCONFIG_SEPARATE = 2;
// COMPRESSION
// TODO(syoyo) more compressin types.
static const int COMPRESSION_NONE = 1;
// ORIENTATION
static const int ORIENTATION_TOPLEFT = 1;
static const int ORIENTATION_TOPRIGHT = 2;
static const int ORIENTATION_BOTRIGHT = 3;
static const int ORIENTATION_BOTLEFT = 4;
static const int ORIENTATION_LEFTTOP = 5;
static const int ORIENTATION_RIGHTTOP = 6;
static const int ORIENTATION_RIGHTBOT = 7;
static const int ORIENTATION_LEFTBOT = 8;
// RESOLUTIONUNIT
static const int RESUNIT_NONE = 1;
static const int RESUNIT_INCH = 2;
static const int RESUNIT_CENTIMETER = 2;
// PHOTOMETRIC
// TODO(syoyo): more photometric types.
static const int PHOTOMETRIC_WHITE_IS_ZERO = 0; // For bilevel and grayscale
static const int PHOTOMETRIC_BLACK_IS_ZERO = 1; // For bilevel and grayscale
static const int PHOTOMETRIC_RGB = 2; // Default
static const int PHOTOMETRIC_CFA = 32893; // DNG ext
static const int PHOTOMETRIC_LINEARRAW = 34892; // DNG ext
// Sample format
static const int SAMPLEFORMAT_UINT = 1; // Default
static const int SAMPLEFORMAT_INT = 2;
static const int SAMPLEFORMAT_IEEEFP = 3; // floating point
struct IFDTag {
unsigned short tag;
unsigned short type;
unsigned int count;
unsigned int offset_or_value;
};
// 12 bytes.
class DNGImage {
public:
DNGImage();
~DNGImage() {}
///
/// Optional: Explicitly specify endian.
/// Must be called before calling other Set methods.
///
void SetBigEndian(bool big_endian);
///
/// Default = 0
///
bool SetSubfileType(bool reduced_image = false, bool page = false,
bool mask = false);
bool SetImageWidth(unsigned int value);
bool SetImageLength(unsigned int value);
bool SetRowsPerStrip(unsigned int value);
bool SetSamplesPerPixel(unsigned short value);
// Set bits for each samples
bool SetBitsPerSample(const unsigned int num_samples,
const unsigned short *values);
bool SetPhotometric(unsigned short value);
bool SetPlanarConfig(unsigned short value);
bool SetOrientation(unsigned short value);
bool SetCompression(unsigned short value);
bool SetSampleFormat(const unsigned int num_samples,
const unsigned short *values);
bool SetXResolution(double value);
bool SetYResolution(double value);
bool SetResolutionUnit(const unsigned short value);
bool SetImageDescription(const std::string &ascii);
bool SetActiveArea(const unsigned int values[4]);
bool SetChromaBlurRadius(double value);
/// Specify black level per sample.
bool SetBlackLevelRational(unsigned int num_samples, const double *values);
/// Specify white level per sample.
bool SetWhiteLevelRational(unsigned int num_samples, const double *values);
/// Set image data
bool SetImageData(const unsigned char *data, const size_t data_len);
/// Set custom field
bool SetCustomFieldLong(const unsigned short tag, const int value);
bool SetCustomFieldULong(const unsigned short tag, const unsigned int value);
size_t GetDataSize() const { return data_os_.str().length(); }
size_t GetStripOffset() const { return data_strip_offset_; }
size_t GetStripBytes() const { return data_strip_bytes_; }
/// Write aux IFD data and strip image data to stream
bool WriteDataToStream(std::ostream *ofs, std::string *err) const;
///
/// Write IFD to stream.
///
/// @param[in] data_base_offset : Byte offset to data
/// @param[in] strip_offset : Byte offset to image strip data
///
/// TODO(syoyo): Support multiple strips
///
bool WriteIFDToStream(const unsigned int data_base_offset,
const unsigned int strip_offset, std::ostream *ofs,
std::string *err) const;
std::string Error() const { return err_; }
private:
std::ostringstream data_os_;
bool swap_endian_;
bool dng_big_endian_;
unsigned short num_fields_;
unsigned int samples_per_pixels_;
unsigned short bits_per_sample_;
// TODO(syoyo): Support multiple strips
size_t data_strip_offset_{0};
size_t data_strip_bytes_{0};
std::string err_; // Error message
std::vector<IFDTag> ifd_tags_;
};
class DNGWriter {
public:
// TODO(syoyo): Use same endian setting with DNGImage.
DNGWriter(bool big_endian);
~DNGWriter() {}
///
/// Add DNGImage.
/// It just retains the pointer of the image, thus
/// application must not free resources until `WriteToFile` has been called.
///
bool AddImage(const DNGImage *image) {
images_.push_back(image);
return true;
}
/// Write DNG to a file.
/// Return error string to `err` when Write() returns false.
/// Returns true upon success.
bool WriteToFile(const char *filename, std::string *err) const;
private:
bool swap_endian_;
bool dng_big_endian_; // Endianness of DNG file.
std::vector<const DNGImage *> images_;
};
} // namespace tinydngwriter
#endif // TINY_DNG_WRITER_H_
#ifdef TINY_DNG_WRITER_IMPLEMENTATION
//
// TIFF format resources.
//
// http://c0de517e.blogspot.jp/2013/07/tiny-hdr-writer.html
// http://paulbourke.net/dataformats/tiff/ and
// http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
//
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
namespace tinydngwriter {
#ifdef __clang__
#pragma clang diagnostic push
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
//
// TinyDNGWriter stores IFD table in the end of file so that offset to
// image data can be easily computed.
//
// +----------------------+
// | header |
// +----------------------+
// | |
// | image & meta 0 |
// | |
// +----------------------+
// | |
// | image & meta 1 |
// | |
// +----------------------+
// ...
// +----------------------+
// | |
// | image & meta N |
// | |
// +----------------------+
// | |
// | IFD 0 |
// | |
// +----------------------+
// | |
// | IFD 1 |
// | |
// +----------------------+
// ...
// +----------------------+
// | |
// | IFD 2 |
// | |
// +----------------------+
//
// From tiff.h
typedef enum {
TIFF_NOTYPE = 0, /* placeholder */
TIFF_BYTE = 1, /* 8-bit unsigned integer */
TIFF_ASCII = 2, /* 8-bit bytes w/ last byte null */
TIFF_SHORT = 3, /* 16-bit unsigned integer */
TIFF_LONG = 4, /* 32-bit unsigned integer */
TIFF_RATIONAL = 5, /* 64-bit unsigned fraction */
TIFF_SBYTE = 6, /* !8-bit signed integer */
TIFF_UNDEFINED = 7, /* !8-bit untyped data */
TIFF_SSHORT = 8, /* !16-bit signed integer */
TIFF_SLONG = 9, /* !32-bit signed integer */
TIFF_SRATIONAL = 10, /* !64-bit signed fraction */
TIFF_FLOAT = 11, /* !32-bit IEEE floating point */
TIFF_DOUBLE = 12, /* !64-bit IEEE floating point */
TIFF_IFD = 13, /* %32-bit unsigned integer (offset) */
TIFF_LONG8 = 16, /* BigTIFF 64-bit unsigned integer */
TIFF_SLONG8 = 17, /* BigTIFF 64-bit signed integer */
TIFF_IFD8 = 18 /* BigTIFF 64-bit unsigned integer (offset) */
} DataType;
const static int kHeaderSize = 8; // TIFF header size.
// floating point to integer rational value conversion
// https://stackoverflow.com/questions/51142275/exact-value-of-a-floating-point-number-as-a-rational
//
// Return error flag
static int DoubleToRational(double x, double *numerator, double *denominator) {
if (!std::isfinite(x)) {
*numerator = *denominator = 0.0;
if (x > 0.0) *numerator = 1.0;
if (x < 0.0) *numerator = -1.0;
return 1;
}
// TIFF Rational use two uint32's, so reduce the bits
int bdigits = FLT_MANT_DIG;
int expo;
*denominator = 1.0;
*numerator = std::frexp(x, &expo) * std::pow(2.0, bdigits);
expo -= bdigits;
if (expo > 0) {
*numerator *= std::pow(2.0, expo);
} else if (expo < 0) {
expo = -expo;
if (expo >= FLT_MAX_EXP - 1) {
*numerator /= std::pow(2.0, expo - (FLT_MAX_EXP - 1));
*denominator *= std::pow(2.0, FLT_MAX_EXP - 1);
return fabs(*numerator) < 1.0;
} else {
*denominator *= std::pow(2.0, expo);
}
}
while ((std::fabs(*numerator) > 0.0) &&
(std::fabs(std::fmod(*numerator, 2)) <
std::numeric_limits<double>::epsilon()) &&
(std::fabs(std::fmod(*denominator, 2)) <
std::numeric_limits<double>::epsilon())) {
*numerator /= 2.0;
*denominator /= 2.0;
}
return 0;
}
static inline bool IsBigEndian() {
unsigned int i = 0x01020304;
char c[4];
memcpy(c, &i, 4);
return (c[0] == 1);
}
static void swap2(unsigned short *val) {
unsigned short tmp = *val;
unsigned char *dst = reinterpret_cast<unsigned char *>(val);
unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
dst[0] = src[1];
dst[1] = src[0];
}
static void swap4(unsigned int *val) {
unsigned int tmp = *val;
unsigned char *dst = reinterpret_cast<unsigned char *>(val);
unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
}
static void swap8(uint64_t *val) {
uint64_t tmp = *val;
unsigned char *dst = reinterpret_cast<unsigned char *>(val);
unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
}
static void Write1(const unsigned char c, std::ostringstream *out) {
unsigned char value = c;
out->write(reinterpret_cast<const char *>(&value), 1);
}
static void Write2(const unsigned short c, std::ostringstream *out,
const bool swap_endian) {
unsigned short value = c;
if (swap_endian) {
swap2(&value);
}
out->write(reinterpret_cast<const char *>(&value), 2);
}
static void Write4(const unsigned int c, std::ostringstream *out,
const bool swap_endian) {
unsigned int value = c;
if (swap_endian) {
swap4(&value);
}
out->write(reinterpret_cast<const char *>(&value), 4);
}
static bool WriteTIFFTag(const unsigned short tag, const unsigned short type,
const unsigned int count, const unsigned char *data,
std::vector<IFDTag> *tags_out,
std::ostringstream *data_out) {
assert(sizeof(IFDTag) ==
12); // FIXME(syoyo): Use static_assert for C++11 compiler
IFDTag ifd;
ifd.tag = tag;
ifd.type = type;
ifd.count = count;
size_t typesize_table[] = {1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4};
size_t len = count * (typesize_table[(type) < 14 ? (type) : 0]);
if (len > 4) {
assert(data_out);
if (!data_out) {
return false;
}
// Store offset value.
unsigned int offset =
static_cast<unsigned int>(data_out->tellp()) + kHeaderSize;
ifd.offset_or_value = offset;
data_out->write(reinterpret_cast<const char *>(data),
static_cast<std::streamsize>(len));
} else {
ifd.offset_or_value = 0;
// less than 4 bytes = store data itself.
if (len == 1) {
unsigned char value = *(data);
memcpy(&(ifd.offset_or_value), &value, sizeof(unsigned char));
} else if (len == 2) {
unsigned short value = *(reinterpret_cast<const unsigned short *>(data));
memcpy(&(ifd.offset_or_value), &value, sizeof(unsigned short));
} else if (len == 4) {
unsigned int value = *(reinterpret_cast<const unsigned int *>(data));
ifd.offset_or_value = value;
} else {
assert(0);
}
}
tags_out->push_back(ifd);
return true;
}
static bool WriteTIFFVersionHeader(std::ostringstream *out, bool big_endian) {
// TODO(syoyo): Support BigTIFF?
// 4d 4d = Big endian. 49 49 = Little endian.
if (big_endian) {
Write1(0x4d, out);
Write1(0x4d, out);
Write1(0x0, out);
Write1(0x2a, out); // Tiff version ID
} else {
Write1(0x49, out);
Write1(0x49, out);
Write1(0x2a, out); // Tiff version ID
Write1(0x0, out);
}
return true;
}
DNGImage::DNGImage()
: dng_big_endian_(true),
num_fields_(0),
samples_per_pixels_(0),
bits_per_sample_(0),
data_strip_offset_{0},
data_strip_bytes_{0} {
swap_endian_ = (IsBigEndian() != dng_big_endian_);
}
void DNGImage::SetBigEndian(bool big_endian) {
dng_big_endian_ = big_endian;
swap_endian_ = (IsBigEndian() != dng_big_endian_);
}
bool DNGImage::SetSubfileType(bool reduced_image, bool page, bool mask) {
unsigned int count = 1;
unsigned int bits = 0;
if (reduced_image) {
bits |= FILETYPE_REDUCEDIMAGE;
}
if (page) {
bits |= FILETYPE_PAGE;
}
if (mask) {
bits |= FILETYPE_MASK;
}
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_SUB_FILETYPE), TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(&bits), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetImageWidth(const unsigned int width) {
unsigned int count = 1;
unsigned int data = width;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_IMAGE_WIDTH), TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetImageLength(const unsigned int length) {
unsigned int count = 1;
const unsigned int data = length;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_IMAGE_LENGTH), TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetRowsPerStrip(const unsigned int rows) {
if (rows == 0) {
return false;
}
unsigned int count = 1;
const unsigned int data = rows;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_ROWS_PER_STRIP), TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetSamplesPerPixel(const unsigned short value) {
if (value > 4) {
return false;
}
unsigned int count = 1;
const unsigned short data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_SAMPLES_PER_PIXEL), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
samples_per_pixels_ = value; // Store SPP for later use.
num_fields_++;
return true;
}
bool DNGImage::SetBitsPerSample(const unsigned int num_samples,
const unsigned short *values) {
// `SetSamplesPerPixel()` must be called in advance and SPP shoud be equal to
// `num_samples`.
if ((num_samples > 0) && (num_samples == samples_per_pixels_)) {
// OK
} else {
err_ += "SetSamplesPerPixel() must be called before SetBitsPerSample().\n";
return false;
}
unsigned short bps = values[0];
std::vector<unsigned short> vs(num_samples);
for (size_t i = 0; i < vs.size(); i++) {
// FIXME(syoyo): Currently bps must be same for all samples
if (bps != values[i]) {
err_ += "BitsPerSample must be same among samples at the moment.\n";
return false;
}
vs[i] = values[i];
// TODO(syoyo): Swap values when writing IFD tag, not here.
if (swap_endian_) {
swap2(&vs[i]);
}
}
unsigned int count = num_samples;
bool ret = WriteTIFFTag(static_cast<unsigned short>(TIFFTAG_BITS_PER_SAMPLE),
TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(vs.data()),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
// Store BPS for later use.
bits_per_sample_ = bps;
num_fields_++;
return true;
}
bool DNGImage::SetPhotometric(const unsigned short value) {
if ((value == PHOTOMETRIC_LINEARRAW) || (value == PHOTOMETRIC_RGB) ||
(value == PHOTOMETRIC_WHITE_IS_ZERO) ||
(value == PHOTOMETRIC_BLACK_IS_ZERO)) {
// OK
} else {
return false;
}
unsigned int count = 1;
const unsigned short data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_PHOTOMETRIC), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetPlanarConfig(const unsigned short value) {
unsigned int count = 1;
if ((value == PLANARCONFIG_CONTIG) || (value == PLANARCONFIG_SEPARATE)) {
// OK
} else {
return false;
}
const unsigned short data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_PLANAR_CONFIG), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetCompression(const unsigned short value) {
unsigned int count = 1;
if ((value == COMPRESSION_NONE)) {
// OK
} else {
return false;
}
const unsigned short data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_COMPRESSION), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetSampleFormat(const unsigned int num_samples,
const unsigned short *values) {
// `SetSamplesPerPixel()` must be called in advance
if ((num_samples > 0) && (num_samples == samples_per_pixels_)) {
// OK
} else {
err_ += "SetSamplesPerPixel() must be called before SetSampleFormat().\n";
return false;
}
unsigned short format = values[0];
std::vector<unsigned short> vs(num_samples);
for (size_t i = 0; i < vs.size(); i++) {
// FIXME(syoyo): Currently format must be same for all samples
if (format != values[i]) {
err_ += "SampleFormat must be same among samples at the moment.\n";
return false;
}
if ((format == SAMPLEFORMAT_UINT) || (format == SAMPLEFORMAT_INT) ||
(format == SAMPLEFORMAT_IEEEFP)) {
// OK
} else {
err_ += "Invalid format value specified for SetSampleFormat().\n";
return false;
}
vs[i] = values[i];
// TODO(syoyo): Swap values when writing IFD tag, not here.
if (swap_endian_) {
swap2(&vs[i]);
}
}
unsigned int count = num_samples;
bool ret = WriteTIFFTag(static_cast<unsigned short>(TIFFTAG_SAMPLEFORMAT),
TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(vs.data()),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetOrientation(const unsigned short value) {
unsigned int count = 1;
if ((value == ORIENTATION_TOPLEFT) || (value == ORIENTATION_TOPRIGHT) ||
(value == ORIENTATION_BOTRIGHT) || (value == ORIENTATION_BOTLEFT) ||
(value == ORIENTATION_LEFTTOP) || (value == ORIENTATION_RIGHTTOP) ||
(value == ORIENTATION_RIGHTBOT) || (value == ORIENTATION_LEFTBOT)) {
// OK
} else {
return false;
}
const unsigned int data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_ORIENTATION), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetBlackLevelRational(unsigned int num_samples,
const double *values) {
// `SetSamplesPerPixel()` must be called in advance and SPP shoud be equal to
// `num_samples`.
if ((num_samples > 0) && (num_samples == samples_per_pixels_)) {
// OK
} else {
return false;
}
std::vector<unsigned int> vs(num_samples * 2);
for (size_t i = 0; i < vs.size(); i++) {
double numerator, denominator;
if (DoubleToRational(values[i], &numerator, &denominator) != 0) {
// Couldn't represent fp value as integer rational value.
return false;
}
vs[2 * i + 0] = static_cast<unsigned int>(numerator);
vs[2 * i + 1] = static_cast<unsigned int>(denominator);
// TODO(syoyo): Swap rational value(8 bytes) when writing IFD tag, not here.
if (swap_endian_) {
swap4(&vs[2 * i + 0]);
swap4(&vs[2 * i + 1]);
}
}
unsigned int count = num_samples;
bool ret = WriteTIFFTag(static_cast<unsigned short>(TIFFTAG_BLACK_LEVEL),
TIFF_RATIONAL, count,
reinterpret_cast<const unsigned char *>(vs.data()),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetWhiteLevelRational(unsigned int num_samples,
const double *values) {
// `SetSamplesPerPixel()` must be called in advance and SPP shoud be equal to
// `num_samples`.
if ((num_samples > 0) && (num_samples == samples_per_pixels_)) {
// OK
} else {
return false;
}
std::vector<unsigned int> vs(num_samples * 2);
for (size_t i = 0; i < vs.size(); i++) {
double numerator, denominator;
if (DoubleToRational(values[i], &numerator, &denominator) != 0) {
// Couldn't represent fp value as integer rational value.
return false;
}
vs[2 * i + 0] = static_cast<unsigned int>(numerator);
vs[2 * i + 1] = static_cast<unsigned int>(denominator);
// TODO(syoyo): Swap rational value(8 bytes) when writing IFD tag, not here.
if (swap_endian_) {
swap4(&vs[2 * i + 0]);
swap4(&vs[2 * i + 1]);
}
}
unsigned int count = num_samples;
bool ret = WriteTIFFTag(static_cast<unsigned short>(TIFFTAG_WHITE_LEVEL),
TIFF_RATIONAL, count,
reinterpret_cast<const unsigned char *>(vs.data()),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetXResolution(const double value) {
double numerator, denominator;
if (DoubleToRational(value, &numerator, &denominator) != 0) {
// Couldn't represent fp value as integer rational value.
return false;
}
unsigned int data[2];
data[0] = static_cast<unsigned int>(numerator);
data[1] = static_cast<unsigned int>(denominator);
// TODO(syoyo): Swap rational value(8 bytes) when writing IFD tag, not here.
if (swap_endian_) {
swap4(&data[0]);
swap4(&data[1]);
}
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_XRESOLUTION), TIFF_RATIONAL, 1,
reinterpret_cast<const unsigned char *>(data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetYResolution(const double value) {
double numerator, denominator;
if (DoubleToRational(value, &numerator, &denominator) != 0) {
// Couldn't represent fp value as integer rational value.
return false;
}
unsigned int data[2];
data[0] = static_cast<unsigned int>(numerator);
data[1] = static_cast<unsigned int>(denominator);
// TODO(syoyo): Swap rational value(8 bytes) when writing IFD tag, not here.
if (swap_endian_) {
swap4(&data[0]);
swap4(&data[1]);
}
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_YRESOLUTION), TIFF_RATIONAL, 1,
reinterpret_cast<const unsigned char *>(data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetResolutionUnit(const unsigned short value) {
unsigned int count = 1;
if ((value == RESUNIT_NONE) || (value == RESUNIT_INCH) ||
(value == RESUNIT_CENTIMETER)) {
// OK
} else {
return false;
}
const unsigned short data = value;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_RESOLUTION_UNIT), TIFF_SHORT, count,
reinterpret_cast<const unsigned char *>(&data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetImageDescription(const std::string &ascii) {
unsigned int count =
static_cast<unsigned int>(ascii.length() + 1); // +1 for '\0'
if (count < 2) {
// empty string
return false;
}
if (count > (1024 * 1024)) {
// too large
return false;
}
bool ret = WriteTIFFTag(static_cast<unsigned short>(TIFFTAG_IMAGEDESCRIPTION),
TIFF_ASCII, count,
reinterpret_cast<const unsigned char *>(ascii.data()),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetActiveArea(const unsigned int values[4]) {
unsigned int count = 4;
const unsigned int *data = values;
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_ACTIVE_AREA), TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(data), &ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetImageData(const unsigned char *data, const size_t data_len) {
if ((data == NULL) || (data_len < 1)) {
return false;
}
data_strip_offset_ = size_t(data_os_.tellp());
data_strip_bytes_ = data_len;
data_os_.write(reinterpret_cast<const char *>(data),
static_cast<std::streamsize>(data_len));
// NOTE: STRIP_OFFSET tag will be written at `WriteIFDToStream()`.
{
unsigned int count = 1;
unsigned int bytes = static_cast<unsigned int>(data_len);
bool ret = WriteTIFFTag(
static_cast<unsigned short>(TIFFTAG_STRIP_BYTE_COUNTS), TIFF_LONG,
count, reinterpret_cast<const unsigned char *>(&bytes), &ifd_tags_,
NULL);
if (!ret) {
return false;
}
num_fields_++;
}
return true;
}
bool DNGImage::SetCustomFieldLong(const unsigned short tag, const int value) {
unsigned int count = 1;
// TODO(syoyo): Check if `tag` value does not conflict with existing TIFF tag
// value.
bool ret = WriteTIFFTag(tag, TIFF_SLONG, count,
reinterpret_cast<const unsigned char *>(&value),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
bool DNGImage::SetCustomFieldULong(const unsigned short tag,
const unsigned int value) {
unsigned int count = 1;
// TODO(syoyo): Check if `tag` value does not conflict with existing TIFF tag
// value.
bool ret = WriteTIFFTag(tag, TIFF_LONG, count,
reinterpret_cast<const unsigned char *>(&value),
&ifd_tags_, &data_os_);
if (!ret) {
return false;
}
num_fields_++;
return true;
}
static bool IFDComparator(const IFDTag &a, const IFDTag &b) {
return (a.tag < b.tag);
}
bool DNGImage::WriteDataToStream(std::ostream *ofs, std::string *err) const {
if ((data_os_.str().length() == 0)) {
if (err) {
(*err) += "Empty IFD data and image data.\n";
}
return false;
}
if ((bits_per_sample_ == 0) || (samples_per_pixels_ == 0)) {
if (err) {
(*err) += "Both BitsPerSample and SamplesPerPixels must be set.\n";
}
return false;
}
std::vector<uint8_t> data(data_os_.str().length());
memcpy(data.data(), data_os_.str().data(), data.size());
if (data_strip_bytes_ == 0) {
// May ok?.
} else {
// FIXME(syoyo): Assume all channels use sample bps
// We may need to swap endian for pixel data.
if (swap_endian_) {
if (bits_per_sample_ == 16) {
size_t n = data_strip_bytes_ / sizeof(uint16_t);
uint16_t *ptr =
reinterpret_cast<uint16_t *>(data.data() + data_strip_offset_);
for (size_t i = 0; i < n; i++) {
swap2(&ptr[i]);
}
} else if (bits_per_sample_ == 32) {
size_t n = data_strip_bytes_ / sizeof(uint32_t);
uint32_t *ptr =
reinterpret_cast<uint32_t *>(data.data() + data_strip_offset_);
for (size_t i = 0; i < n; i++) {
swap4(&ptr[i]);
}
} else if (bits_per_sample_ == 64) {
size_t n = data_strip_bytes_ / sizeof(uint64_t);
uint64_t *ptr =
reinterpret_cast<uint64_t *>(data.data() + data_strip_offset_);
for (size_t i = 0; i < n; i++) {
swap8(&ptr[i]);
}
}
}
}
ofs->write(reinterpret_cast<const char *>(data.data()),
static_cast<std::streamsize>(data.size()));
return true;
}
bool DNGImage::WriteIFDToStream(const unsigned int data_base_offset,
const unsigned int strip_offset,
std::ostream *ofs, std::string *err) const {
if ((num_fields_ == 0) || (ifd_tags_.size() < 1)) {
if (err) {
(*err) += "No TIFF Tags.\n";
}
return false;
}
// add STRIP_OFFSET tag and sort IFD tags.
std::vector<IFDTag> tags = ifd_tags_;
{
// For STRIP_OFFSET we need the actual offset value to data(image),
// thus write STRIP_OFFSET here.
unsigned int offset = strip_offset + kHeaderSize;
IFDTag ifd;
ifd.tag = TIFFTAG_STRIP_OFFSET;
ifd.type = TIFF_LONG;
ifd.count = 1;
ifd.offset_or_value = offset;
tags.push_back(ifd);
}
// TIFF expects IFD tags are sorted.
std::sort(tags.begin(), tags.end(), IFDComparator);
std::ostringstream ifd_os;
unsigned short num_fields = static_cast<unsigned short>(tags.size());
Write2(num_fields, &ifd_os, swap_endian_);
{
size_t typesize_table[] = {1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4};
for (size_t i = 0; i < tags.size(); i++) {
const IFDTag &ifd = tags[i];
Write2(ifd.tag, &ifd_os, swap_endian_);
Write2(ifd.type, &ifd_os, swap_endian_);
Write4(ifd.count, &ifd_os, swap_endian_);
size_t len =
ifd.count * (typesize_table[(ifd.type) < 14 ? (ifd.type) : 0]);
if (len > 4) {
// Store offset value.
unsigned int ifd_offt = ifd.offset_or_value + data_base_offset;
Write4(ifd_offt, &ifd_os, swap_endian_);
} else {
// less than 4 bytes = store data itself.
if (len == 1) {
const unsigned char value =
*(reinterpret_cast<const unsigned char *>(&ifd.offset_or_value));
Write1(value, &ifd_os);
unsigned char pad = 0;
Write1(pad, &ifd_os);
Write1(pad, &ifd_os);
Write1(pad, &ifd_os);
} else if (len == 2) {
const unsigned short value =
*(reinterpret_cast<const unsigned short *>(&ifd.offset_or_value));
Write2(value, &ifd_os, swap_endian_);
const unsigned short pad = 0;
Write2(pad, &ifd_os, swap_endian_);
} else if (len == 4) {
const unsigned int value =
*(reinterpret_cast<const unsigned int *>(&ifd.offset_or_value));
Write4(value, &ifd_os, swap_endian_);
} else {
assert(0);
}
}
}
ofs->write(ifd_os.str().c_str(),
static_cast<std::streamsize>(ifd_os.str().length()));
}
return true;
}
// -------------------------------------------
DNGWriter::DNGWriter(bool big_endian) : dng_big_endian_(big_endian) {
swap_endian_ = (IsBigEndian() != dng_big_endian_);
}
bool DNGWriter::WriteToFile(const char *filename, std::string *err) const {
std::ofstream ofs(filename, std::ostream::binary);
if (!ofs) {
if (err) {
(*err) = "Failed to open file.\n";
}
return false;
}
std::ostringstream header;
bool ret = WriteTIFFVersionHeader(&header, dng_big_endian_);
if (!ret) {
return false;
}
if (images_.size() == 0) {
if (err) {
(*err) = "No image added for writing.\n";
}
return false;
}
// 1. Compute offset and data size(exclude TIFF header bytes)
size_t data_len = 0;
size_t strip_offset = 0;
std::vector<size_t> data_offset_table;
std::vector<size_t> strip_offset_table;
for (size_t i = 0; i < images_.size(); i++) {
strip_offset = data_len + images_[i]->GetStripOffset();
data_offset_table.push_back(data_len);
strip_offset_table.push_back(strip_offset);
data_len += images_[i]->GetDataSize();
}
// 2. Write offset to ifd table.
const unsigned int ifd_offset =
kHeaderSize + static_cast<unsigned int>(data_len);
Write4(ifd_offset, &header, swap_endian_);
assert(header.str().length() == 8);
// std::cout << "ifd_offset " << ifd_offset << std::endl;
// std::cout << "data_len " << data_os_.str().length() << std::endl;
// std::cout << "ifd_len " << ifd_os_.str().length() << std::endl;
// std::cout << "swap endian " << swap_endian_ << std::endl;
// 3. Write header
ofs.write(header.str().c_str(),
static_cast<std::streamsize>(header.str().length()));
// 4. Write image and meta data
// TODO(syoyo): Write IFD first, then image/meta data
for (size_t i = 0; i < images_.size(); i++) {
bool ok = images_[i]->WriteDataToStream(&ofs, err);
if (!ok) {
return false;
}
}
// 5. Write IFD entries;
for (size_t i = 0; i < images_.size(); i++) {
bool ok = images_[i]->WriteIFDToStream(
static_cast<unsigned int>(data_offset_table[i]),
static_cast<unsigned int>(strip_offset_table[i]), &ofs, err);
if (!ok) {
return false;
}
unsigned int next_ifd_offset =
static_cast<unsigned int>(ofs.tellp()) + sizeof(unsigned int);
if (i == (images_.size() - 1)) {
// Write zero as IFD offset(= end of data)
next_ifd_offset = 0;
}
if (swap_endian_) {
swap4(&next_ifd_offset);
}
ofs.write(reinterpret_cast<const char *>(&next_ifd_offset), 4);
}
return true;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} // namespace tinydngwriter
#endif // TINY_DNG_WRITER_IMPLEMENTATION
|