libdng.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. #include "libdng.h"
  2. #include "dng.h"
  3. #include "mode.h"
  4. #include "repack.h"
  5. #include <stdio.h>
  6. #include <tiffio.h>
  7. #include <sys/time.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #define DNG_SUBFILETYPE_ORIGINAL 0
  12. #define DNG_SUBFILETYPE_THUMBNAIL 1
  13. #define DNG_SUBFILETYPE_ALPHAMASK 4
  14. #define DNG_SUBFILETYPE_THUMBNAIL_ALPHAMASK 5
  15. #define DNG_SUBFILETYPE_THUMBNAIL_EXTRA 0x10001
  16. static void
  17. register_dng_tags(TIFF *tif)
  18. {
  19. TIFFMergeFieldInfo(tif,
  20. custom_dng_fields,
  21. sizeof(custom_dng_fields) / sizeof(custom_dng_fields[0]));
  22. }
  23. int
  24. libdng_init()
  25. {
  26. TIFFSetTagExtender(register_dng_tags);
  27. return 0;
  28. }
  29. void
  30. libdng_new(libdng_info *dng)
  31. {
  32. dng->orientation = 1;
  33. dng->bayer_pattern_dimensions[0] = 2;
  34. dng->bayer_pattern_dimensions[1] = 2;
  35. for (size_t i = 0; i < 9; i++) {
  36. dng->color_matrix_1[i] = 0.0f;
  37. dng->color_matrix_2[i] = 0.0f;
  38. }
  39. dng->color_matrix_1[0] = 1.0f;
  40. dng->color_matrix_1[4] = 1.0f;
  41. dng->color_matrix_1[8] = 1.0f;
  42. dng->cfapattern[0] = 0;
  43. dng->cfapattern[1] = 1;
  44. dng->cfapattern[2] = 1;
  45. dng->cfapattern[3] = 2;
  46. dng->neutral[0] = 1.0f;
  47. dng->neutral[1] = 1.0f;
  48. dng->neutral[2] = 1.0f;
  49. dng->analogbalance[0] = 1.0f;
  50. dng->analogbalance[1] = 1.0f;
  51. dng->analogbalance[2] = 1.0f;
  52. dng->exposure_time = 0.0f;
  53. dng->iso = 0;
  54. dng->fnumber = 0.0f;
  55. dng->crop_factor = 1.0f;
  56. dng->focal_length = 0.0f;
  57. dng->frame_rate = 0.0f;
  58. }
  59. int
  60. libdng_set_mode_from_index(libdng_info *dng, int index)
  61. {
  62. uint32_t cfa = dng_cfa_from_mode(index);
  63. dng->cfapattern[0] = (cfa >> 24) & 0xFF;
  64. dng->cfapattern[1] = (cfa >> 16) & 0xFF;
  65. dng->cfapattern[2] = (cfa >> 8) & 0xFF;
  66. dng->cfapattern[3] = (cfa >> 0) & 0xFF;
  67. dng->needs_repack = dng_mode_needs_repack(index);
  68. dng->bit_depth = dng_bitdepth_from_mode(index);
  69. dng->whitelevel = (1 << dng->bit_depth) - 1;
  70. return 1;
  71. }
  72. int
  73. libdng_set_mode_from_pixfmt(libdng_info *dng, uint32_t pixfmt)
  74. {
  75. int index = dng_mode_from_pixfmt(pixfmt);
  76. if (index == 0) {
  77. fprintf(stderr, "libdng: Invalid pixfmt '%c%c%c%c'\n", pixfmt & 0xFF, pixfmt >> 8 & 0xFF, pixfmt >> 16 & 0xFF,
  78. pixfmt >> 24 & 0xFF);
  79. return 0;
  80. }
  81. return libdng_set_mode_from_index(dng, index);
  82. }
  83. int
  84. libdng_set_mode_from_name(libdng_info *dng, const char *name)
  85. {
  86. int index = dng_mode_from_name(name);
  87. if (index == 0) {
  88. fprintf(stderr, "Invalid mode '%s'\n", name);
  89. return 0;
  90. }
  91. return libdng_set_mode_from_index(dng, index);
  92. }
  93. int
  94. libdng_set_make_model(libdng_info *dng, const char *make, const char *model)
  95. {
  96. if (dng == NULL)
  97. return 0;
  98. dng->camera_make = strdup(make);
  99. dng->camera_model = strdup(model);
  100. return 1;
  101. }
  102. int
  103. libdng_set_software(libdng_info *dng, const char *software)
  104. {
  105. if (dng == NULL)
  106. return 0;
  107. dng->software = strdup(software);
  108. return 1;
  109. }
  110. void
  111. libdng_free(libdng_info *dng)
  112. {
  113. if (dng->camera_make != NULL)
  114. free(dng->camera_make);
  115. if (dng->camera_model != NULL)
  116. free(dng->camera_model);
  117. if (dng->software != NULL)
  118. free(dng->software);
  119. }
  120. int
  121. libdng_set_datetime(libdng_info *dng, struct tm time)
  122. {
  123. if (dng == NULL)
  124. return 0;
  125. memcpy(&dng->datetime, &time, sizeof(struct tm));
  126. return 1;
  127. }
  128. int
  129. libdng_set_datetime_now(libdng_info *dng)
  130. {
  131. if (dng == NULL)
  132. return 0;
  133. time_t rawtime;
  134. time(&rawtime);
  135. dng->datetime = *(localtime(&rawtime));
  136. return 1;
  137. }
  138. int
  139. libdng_set_orientation(libdng_info *dng, uint16_t orientation)
  140. {
  141. if (dng == NULL)
  142. return 0;
  143. dng->orientation = orientation;
  144. return 1;
  145. }
  146. int
  147. libdng_set_neutral(libdng_info *dng, float red, float green, float blue)
  148. {
  149. if (dng == NULL)
  150. return 0;
  151. dng->neutral[0] = red;
  152. dng->neutral[1] = green;
  153. dng->neutral[2] = blue;
  154. return 1;
  155. }
  156. int
  157. libdng_set_analog_balance(libdng_info *dng, float red, float green, float blue)
  158. {
  159. if (dng == NULL)
  160. return 0;
  161. dng->analogbalance[0] = red;
  162. dng->analogbalance[1] = green;
  163. dng->analogbalance[2] = blue;
  164. return 1;
  165. }
  166. int
  167. libdng_set_exposure_program(libdng_info *dng, uint16_t mode)
  168. {
  169. if (dng == NULL)
  170. return 0;
  171. if (mode > 8)
  172. return 0;
  173. dng->exposure_program = mode;
  174. return 1;
  175. }
  176. int
  177. libdng_set_exposure_time(libdng_info *dng, float seconds)
  178. {
  179. if (dng == NULL)
  180. return 0;
  181. if (seconds < 0.0f)
  182. return 0;
  183. dng->exposure_time = seconds;
  184. return 1;
  185. }
  186. int
  187. libdng_set_iso(libdng_info *dng, uint32_t isospeed)
  188. {
  189. if (dng == NULL)
  190. return 0;
  191. dng->iso = isospeed;
  192. return 1;
  193. }
  194. int
  195. libdng_set_fnumber(libdng_info *dng, float fnumber)
  196. {
  197. if (dng == NULL)
  198. return 0;
  199. if (fnumber < 0.0f)
  200. return 0;
  201. dng->fnumber = fnumber;
  202. return 1;
  203. }
  204. int
  205. libdng_set_focal_length(libdng_info *dng, float focal_length, float crop_factor)
  206. {
  207. if (dng == NULL)
  208. return 0;
  209. if (focal_length < 0.0f)
  210. return 0;
  211. if (crop_factor < 0.0f)
  212. return 0;
  213. dng->focal_length = focal_length;
  214. dng->crop_factor = crop_factor;
  215. return 1;
  216. }
  217. int
  218. libdng_set_frame_rate(libdng_info *dng, float framerate)
  219. {
  220. if (dng == NULL)
  221. return 0;
  222. dng->frame_rate = framerate;
  223. return 1;
  224. }
  225. int
  226. libdng_write(libdng_info *dng, const char *path, unsigned int width, unsigned int height, const uint8_t *data,
  227. size_t length)
  228. {
  229. return libdng_write_with_thumbnail(dng, path, width, height, data, length, 0, 0, NULL, 0);
  230. }
  231. int
  232. libdng_write_with_thumbnail(libdng_info *dng, const char *path, unsigned int width, unsigned int height,
  233. const uint8_t *data, size_t length, unsigned int thumb_width, unsigned int thumb_height, const uint8_t *thumb,
  234. size_t thumb_length)
  235. {
  236. uint8_t *raw_frame = (uint8_t *) data;
  237. if (dng->needs_repack) {
  238. raw_frame = malloc(length);
  239. dng_repack(data, raw_frame, width, height, dng->bit_depth);
  240. }
  241. TIFF *tif = TIFFOpen(path, "w");
  242. if (!tif) {
  243. return 0;
  244. }
  245. char datetime[20] = {0};
  246. if (dng->datetime.tm_year) {
  247. strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &dng->datetime);
  248. }
  249. uint64_t ifd0_offsets[] = {0L};
  250. // When not supplying a thumbnail generate a black thumb with 1/16th of the
  251. // resolution of the full picture
  252. if (thumb_length == 0) {
  253. thumb_width = width >> 4;
  254. thumb_height = height >> 4;
  255. }
  256. // First IFD describes the thumbnail and contains most of the metadata
  257. // Tags are in numerical order
  258. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, DNG_SUBFILETYPE_THUMBNAIL);
  259. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, thumb_width);
  260. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, thumb_height);
  261. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  262. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  263. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  264. TIFFSetField(tif, TIFFTAG_ORIENTATION, dng->orientation);
  265. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  266. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  267. TIFFSetField(tif, DNGTAG_COLOR_MATRIX_1, 9, dng->color_matrix_1);
  268. TIFFSetField(tif, DNGTAG_ASSHOTNEUTRAL, 3, dng->neutral);
  269. TIFFSetField(tif, DNGTAG_ANALOGBALANCE, 3, dng->analogbalance);
  270. if (dng->camera_make != NULL)
  271. TIFFSetField(tif, TIFFTAG_MAKE, dng->camera_make);
  272. if (dng->camera_model != NULL)
  273. TIFFSetField(tif, TIFFTAG_MODEL, dng->camera_model);
  274. if (dng->software != NULL)
  275. TIFFSetField(tif, TIFFTAG_SOFTWARE, dng->software);
  276. if (dng->datetime.tm_year) {
  277. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  278. }
  279. TIFFSetField(tif, DNGTAG_DNGVERSION, "\001\004\0\0");
  280. char ucm[255];
  281. if (dng->unique_camera_model != NULL) {
  282. snprintf(ucm, sizeof(ucm), "%s", dng->unique_camera_model);
  283. } else if (dng->camera_make == NULL && dng->camera_model == NULL) {
  284. snprintf(ucm, sizeof(ucm), "%s", "LibDNG");
  285. } else {
  286. snprintf(ucm, sizeof(ucm), "%s %s", dng->camera_make, dng->camera_model);
  287. }
  288. TIFFSetField(tif, DNGTAG_UNIQUECAMERAMODEL, ucm);
  289. if (dng->frame_rate != 0.0f) {
  290. TIFFSetField(tif, DNGTAG_FRAMERATE, 1, &dng->frame_rate);
  291. }
  292. TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &ifd0_offsets);
  293. if (thumb_length == 0) {
  294. // Generate a single black scanline and write it
  295. unsigned char *buf = (unsigned char *) calloc(1, thumb_width * 3);
  296. for (int row = 0; row < thumb_height; row++) {
  297. TIFFWriteScanline(tif, buf, row, 0);
  298. }
  299. free(buf);
  300. } else {
  301. // Write the supplied thumbnail
  302. unsigned int t_stride = thumb_width;
  303. for (int row = 0; row < thumb_height; row++) {
  304. TIFFWriteScanline(tif, (void *) thumb + (row * t_stride), row, 0);
  305. }
  306. }
  307. if (!TIFFWriteDirectory(tif)) {
  308. return 0;
  309. }
  310. // Define the raw data IFD
  311. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, DNG_SUBFILETYPE_ORIGINAL);
  312. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
  313. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
  314. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
  315. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  316. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  317. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  318. #if (TIFFLIB_VERSION > 20230000 && TIFFLIB_VERSION < 20240321)
  319. TIFFSetField(tif, DNGTAG_CFAREPEATPATTERNDIM, 2, dng->bayer_pattern_dimensions);
  320. #else
  321. TIFFSetField(tif, DNGTAG_CFAREPEATPATTERNDIM, dng->bayer_pattern_dimensions);
  322. #endif
  323. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, dng->bit_depth);
  324. TIFFSetField(tif, DNGTAG_CFAPATTERN, 4, dng->cfapattern);
  325. TIFFSetField(tif, DNGTAG_WHITELEVEL, 1, &dng->whitelevel);
  326. unsigned int stride = width;
  327. for (int row = 0; row < height; row++) {
  328. TIFFWriteScanline(tif, (void *) raw_frame + (row * stride), row, 0);
  329. }
  330. if (!TIFFWriteDirectory(tif)) {
  331. return 0;
  332. }
  333. if (TIFFCreateEXIFDirectory(tif) != 0) {
  334. fprintf(stderr, "Could not create EXIF\n");
  335. return 0;
  336. }
  337. if (dng->datetime.tm_year) {
  338. if (!TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime)) {
  339. fprintf(stderr, "Could not write datetimeoriginal\n");
  340. }
  341. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  342. }
  343. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, dng->exposure_program);
  344. if (dng->exposure_time > 0) {
  345. TIFFSetField(tif, EXIFTAG_EXPOSURETIME, dng->exposure_time);
  346. }
  347. if (dng->iso > 0) {
  348. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, &dng->iso);
  349. }
  350. if (dng->fnumber > 0) {
  351. TIFFSetField(tif, EXIFTAG_FNUMBER, dng->fnumber);
  352. }
  353. if (dng->focal_length > 0) {
  354. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, dng->focal_length);
  355. if (dng->crop_factor != 1.0f) {
  356. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM, (uint16_t) (dng->focal_length * dng->crop_factor));
  357. }
  358. }
  359. uint64_t exif_offset = 0;
  360. if (!TIFFWriteCustomDirectory(tif, &exif_offset)) {
  361. fprintf(stderr, "Can't write EXIF\n");
  362. return 0;
  363. }
  364. TIFFFreeDirectory(tif);
  365. // Update exif pointer
  366. TIFFSetDirectory(tif, 0);
  367. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  368. TIFFRewriteDirectory(tif);
  369. TIFFClose(tif);
  370. if (dng->needs_repack) {
  371. free(raw_frame);
  372. }
  373. return 1;
  374. }
  375. int
  376. libdng_read(libdng_info *dng, const char *path)
  377. {
  378. TIFF *tif = TIFFOpen(path, "r");
  379. if (!tif) {
  380. return 0;
  381. }
  382. unsigned short count;
  383. char *cvalues;
  384. float *fvalues;
  385. uint8_t *u8values;
  386. uint32_t *u32values;
  387. // Reading the "main" image which is the thumbnail
  388. TIFFGetField(tif, TIFFTAG_ORIENTATION, &dng->orientation);
  389. if (TIFFGetField(tif, TIFFTAG_MAKE, &cvalues) == 1) {
  390. dng->camera_make = strdup(cvalues);
  391. }
  392. if (TIFFGetField(tif, TIFFTAG_MODEL, &cvalues) == 1) {
  393. dng->camera_model = strdup(cvalues);
  394. }
  395. if (TIFFGetField(tif, TIFFTAG_UNIQUECAMERAMODEL, &dng->unique_camera_model) == 1) {
  396. dng->unique_camera_model = strdup(cvalues);
  397. }
  398. if (TIFFGetField(tif, TIFFTAG_SOFTWARE, &cvalues) == 1) {
  399. dng->software = strdup(cvalues);
  400. }
  401. if (TIFFGetField(tif, TIFFTAG_DATETIME, &cvalues) == 1) {
  402. strptime(cvalues, "%Y:%m:%d %H:%M:%S", &dng->datetime);
  403. }
  404. TIFFGetField(tif, TIFFTAG_ORIENTATION, &dng->orientation);
  405. if (TIFFGetField(tif, DNGTAG_ASSHOTNEUTRAL, &count, &fvalues) == 1) {
  406. for (int i = 0; i < count; i++) {
  407. dng->neutral[i] = fvalues[i];
  408. }
  409. }
  410. if (TIFFGetField(tif, DNGTAG_ANALOGBALANCE, &count, &fvalues) == 1) {
  411. for (int i = 0; i < count; i++) {
  412. dng->analogbalance[i] = fvalues[i];
  413. }
  414. }
  415. if (TIFFGetField(tif, DNGTAG_COLOR_MATRIX_1, &count, &fvalues) == 1) {
  416. for (int i = 0; i < count; i++) {
  417. dng->color_matrix_1[i] = fvalues[i];
  418. }
  419. }
  420. if (TIFFGetField(tif, DNGTAG_COLOR_MATRIX_2, &count, &fvalues) == 1) {
  421. for (int i = 0; i < count; i++) {
  422. dng->color_matrix_2[i] = fvalues[i];
  423. }
  424. }
  425. if (TIFFGetField(tif, DNGTAG_FORWARDMATRIX1, &count, &fvalues) == 1) {
  426. for (int i = 0; i < count; i++) {
  427. dng->forward_matrix_1[i] = fvalues[i];
  428. }
  429. }
  430. if (TIFFGetField(tif, DNGTAG_FORWARDMATRIX2, &count, &fvalues) == 1) {
  431. for (int i = 0; i < count; i++) {
  432. dng->forward_matrix_2[i] = fvalues[i];
  433. }
  434. }
  435. int subifd_count = 0;
  436. void *ptr;
  437. toff_t subifd_offsets[2];
  438. toff_t exif_offset = 0;
  439. TIFFGetField(tif, TIFFTAG_EXIFIFD, &exif_offset);
  440. TIFFGetField(tif, TIFFTAG_SUBIFD, &subifd_count, &ptr);
  441. memcpy(subifd_offsets, ptr, subifd_count * sizeof(subifd_offsets[0]));
  442. TIFFSetSubDirectory(tif, subifd_offsets[0]);
  443. TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &dng->bit_depth);
  444. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dng->width);
  445. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dng->height);
  446. if (TIFFGetField(tif, DNGTAG_WHITELEVEL, &count, &u32values) == 1) {
  447. dng->whitelevel = u32values[0];
  448. } else {
  449. dng->whitelevel = (1 << dng->bit_depth) - 1;
  450. }
  451. if (TIFFGetField(tif, DNGTAG_CFAPATTERN, &count, &u8values) == 1) {
  452. if (count > 4) {
  453. fprintf(stderr, "overflow in CFAPATTERN length %d > 4\n", count);
  454. return 0;
  455. }
  456. for (int i = 0; i < count; i++) {
  457. dng->cfapattern[i] = u8values[i];
  458. }
  459. }
  460. if (exif_offset != 0) {
  461. TIFFReadEXIFDirectory(tif, exif_offset);
  462. TIFFGetField(tif, EXIFTAG_EXPOSURETIME, &dng->exposure_time);
  463. TIFFGetField(tif, EXIFTAG_EXPOSUREPROGRAM, &dng->exposure_program);
  464. TIFFGetField(tif, EXIFTAG_FNUMBER, &dng->fnumber);
  465. TIFFGetField(tif, EXIFTAG_FOCALLENGTH, &dng->focal_length);
  466. uint16_t fffocallength;
  467. if (TIFFGetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM, &fffocallength) == 1) {
  468. if (fffocallength > 0.0f) {
  469. dng->crop_factor = (float) fffocallength / dng->focal_length;
  470. }
  471. }
  472. }
  473. TIFFClose(tif);
  474. return 1;
  475. }
  476. int
  477. libdng_read_image(libdng_info *dng, const char *path, uint8_t index, uint8_t **data, size_t *length, uint32_t *width,
  478. uint32_t *height)
  479. {
  480. TIFF *tif = TIFFOpen(path, "r");
  481. if (!tif) {
  482. return 0;
  483. }
  484. if (index == 1) {
  485. int subifd_count = 0;
  486. void *ptr;
  487. toff_t subifd_offsets[2];
  488. TIFFGetField(tif, TIFFTAG_SUBIFD, &subifd_count, &ptr);
  489. memcpy(subifd_offsets, ptr, subifd_count * sizeof(subifd_offsets[0]));
  490. TIFFSetSubDirectory(tif, subifd_offsets[0]);
  491. }
  492. uint32_t samples_per_pixel;
  493. uint32_t bits_per_sample;
  494. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width);
  495. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height);
  496. TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
  497. TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
  498. tsize_t scanline_size = TIFFScanlineSize(tif);
  499. float bytes_per_pixel = (float) samples_per_pixel * (float) bits_per_sample / 8.0f;
  500. (*length) = (uint32_t) ((float) (*width) * (float) (*height) * bytes_per_pixel);
  501. (*data) = malloc(*length);
  502. if (*data == NULL) {
  503. fprintf(stderr, "Could not allocate memory in libdng_read_image\n");
  504. return 0;
  505. }
  506. for (uint32_t y = 0; y < *height; y++) {
  507. TIFFReadScanline(tif, (*data) + (y * scanline_size), y, 0);
  508. }
  509. TIFFClose(tif);
  510. return 1;
  511. }