api.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. API
  2. ===
  3. .. contents:: Table of Contents
  4. :depth: 3
  5. Initialisation
  6. --------------
  7. The library mostly works on the libdng_info objects. There's only a single
  8. initialisation function
  9. int libdng_init()
  10. ^^^^^^^^^^^^^^^^^
  11. This function should be called exactly once before using any of the other
  12. functionality of the library. The purpose of this call is to register new
  13. tags into libtiff which is required before any other interaction.
  14. DNG objects
  15. -----------
  16. All the state for writing is stored in the :code:`libdng_info` struct. These
  17. functions work by manipulating this instance.
  18. void libdng_new(libdng_info \*dng)
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. Initialize the value of a libdng_info struct for further operation. This needs
  21. to be cleaned up with libdng_free.
  22. .. code-block:: c
  23. libdng_info dng = {0};
  24. libdng_new(&dng);
  25. void libdng_free(libdng_info \*dng)
  26. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  27. Clean up the allocations of :code:`libdng_new`
  28. .. code-block:: c
  29. libdng_info dng = {0};
  30. libdng_new(&dng);
  31. // Do important stuff here
  32. libdng_free(&dng);
  33. .. _set_mode_from_name:
  34. int libdng_set_mode_from_name(libdng_info \*dng, const char \*name)
  35. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  36. Set the pixel format for the raw data that will be written into the DNG. This
  37. configures the CFA pattern, bit depth and whitelevel metadata. The valid names
  38. are the V4L2 fourcc codes and the constant names.
  39. .. code-block:: c
  40. libdng_info dng = {0};
  41. libdng_new(&dng);
  42. libdng_set_mode_from_name(&dng, "SRGGB10P");
  43. libdng_free(&dng);
  44. Affected tags:
  45. - TIFFTAG_CFAPATTERN
  46. - TIFFTAG_WHITELEVEL
  47. - TIFFTAG_BITSPERSAMPLE
  48. Supported values:
  49. +--------+---------------+-------+
  50. | fourcc | constant name | Depth |
  51. +========+===============+=======+
  52. | RGGB | SRGGB8 | 8 |
  53. +--------+---------------+-------+
  54. | GRBG | SGRBG8 | 8 |
  55. +--------+---------------+-------+
  56. | GBRG | SGBRG8 | 8 |
  57. +--------+---------------+-------+
  58. | BGGR | SBGGR8 | 8 |
  59. +--------+---------------+-------+
  60. | RG10 | SRGGB10 | 10 |
  61. +--------+---------------+-------+
  62. | BA10 | SGRBG10 | 10 |
  63. +--------+---------------+-------+
  64. | GB10 | SGBRG10 | 10 |
  65. +--------+---------------+-------+
  66. | BG10 | SBGGR10 | 10 |
  67. +--------+---------------+-------+
  68. | pRAA | SRGGB10P | 10 |
  69. +--------+---------------+-------+
  70. | pgAA | SGRBG10P | 10 |
  71. +--------+---------------+-------+
  72. | pGAA | SGBRG10P | 10 |
  73. +--------+---------------+-------+
  74. | pBAA | SBGGR10P | 10 |
  75. +--------+---------------+-------+
  76. | RG12 | SRGGB12 | 12 |
  77. +--------+---------------+-------+
  78. | BA12 | SGRBG12 | 12 |
  79. +--------+---------------+-------+
  80. | GB12 | SGBRG12 | 12 |
  81. +--------+---------------+-------+
  82. | BG12 | SBGGR12 | 12 |
  83. +--------+---------------+-------+
  84. | pRCC | SRGGB12P | 12 |
  85. +--------+---------------+-------+
  86. | pgCC | SGRBG12P | 12 |
  87. +--------+---------------+-------+
  88. | pGCC | SGBRG12P | 12 |
  89. +--------+---------------+-------+
  90. | pBCC | SBGGR12P | 12 |
  91. +--------+---------------+-------+
  92. | RG16 | SRGGB16 | 16 |
  93. +--------+---------------+-------+
  94. | GR16 | SGRBG16 | 16 |
  95. +--------+---------------+-------+
  96. | GB16 | SGBRG16 | 16 |
  97. +--------+---------------+-------+
  98. | BYR2 | SBGGR16 | 16 |
  99. +--------+---------------+-------+
  100. int libdng_set_mode_from_pixfmt(libdng_info \*dng, uint32_t pixfmt)
  101. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  102. This function performs the same tasks as :ref:`libdng_set_mode_from_name <set_mode_from_name>` but
  103. uses the numeric fourcc value instead of a string. This is for passing through
  104. the format from the V4L2 subsystem directly.
  105. .. code-block:: c
  106. libdng_info dng = {0};
  107. libdng_new(&dng);
  108. libdng_set_mode_from_pixfmt(&dng, V4L2_PIX_FMT_SGBRG12P);
  109. libdng_free(&dng);
  110. Affected tags:
  111. - TIFFTAG_CFAPATTERN
  112. - TIFFTAG_WHITELEVEL
  113. - TIFFTAG_BITSPERSAMPLE
  114. int libdng_set_make_model(libdng_info \*dng, const char \*make, const char \*model)
  115. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  116. Set the make and model name for the camera or other digitizing equipment that
  117. produced the raw data. The make and model are also concatinated together to
  118. produce the "unique camera model" field that is usually shown to end-users as
  119. the camera model.
  120. .. code-block:: c
  121. libdng_info dng = {0};
  122. libdng_new(&dng);
  123. libdng_set_make_model(&dng, "Nikon", "D3300");
  124. libdng_free(&dng);
  125. Affected tags:
  126. - TIFFTAG_MAKE
  127. - TIFFTAG_MODEL
  128. - TIFFTAG_UNIQUECAMERAMODEL
  129. int libdng_set_software(libdng_info \*dng, const char \*software)
  130. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  131. Set the name and version number of the software used to produce the image. On
  132. phones this value is usually set to the camera application.
  133. .. code-block:: c
  134. libdng_info dng = {0};
  135. libdng_new(&dng);
  136. libdng_set_software(&dng, "Megapixels 2.0");
  137. libdng_free(&dng);
  138. Affected tags:
  139. - TIFFTAG_SOFTWARE
  140. .. _set_datetime:
  141. int libdng_set_datetime(libdng_info \*dng, struct tm time)
  142. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  143. Set the date and time the picture was taken or digitized. This is stored as both
  144. TIFF and EXIF metadata. Instead of specifying a :code:`tm` struct the
  145. :ref:`libdng_set_datetime_now <set_datetime_now>` function can be used to
  146. directly use the current time.
  147. .. code-block:: c
  148. libdng_info dng = {0};
  149. libdng_new(&dng);
  150. time_t rawtime;
  151. time(&rawtime);
  152. struct tm now = *(localtime(&rawtime));
  153. libdng_set_datetime(&dng, now);
  154. libdng_free(&dng);
  155. Affected tags:
  156. - TIFFTAG_DATETIME
  157. - EXIFTAG_DATETIMEORIGINAL
  158. - EXIFTAG_DATETIMEDIGITIZED
  159. .. _set_datetime_now:
  160. int libdng_set_datetime_now(libdng_info \*dng)
  161. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  162. Set the date and time the picture was taken or digitized to the current date and
  163. time. This is a shortcut method for :ref:`libdng_set_datetime <set_datetime>`.
  164. .. code-block:: c
  165. libdng_info dng = {0};
  166. libdng_new(&dng);
  167. libdng_set_datetime_now(&dng);
  168. libdng_free(&dng);
  169. Affected tags:
  170. - TIFFTAG_DATETIME
  171. - EXIFTAG_DATETIMEORIGINAL
  172. - EXIFTAG_DATETIMEDIGITIZED
  173. .. _set_orientation:
  174. int libdng_set_orientation(libdng_info \*dng, uint16_t orientation)
  175. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  176. Set the date and time the picture was taken or digitized to the current date and
  177. time. This is a shortcut method for :ref:`libdng_set_datetime <set_datetime>`.
  178. .. code-block:: c
  179. libdng_info dng = {0};
  180. libdng_new(&dng);
  181. libdng_set_datetime_now(&dng);
  182. libdng_free(&dng);
  183. Affected tags:
  184. - TIFFTAG_DATETIME
  185. - EXIFTAG_DATETIMEORIGINAL
  186. - EXIFTAG_DATETIMEDIGITIZED
  187. .. _set_neutral:
  188. int libdng_set_neutral(libdng_info \*dng, float r, float g, float b)
  189. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  190. Set the whitebalance of the picture. The neutral whitepoint gains are the RGB
  191. gains that will be applied on top of the raw debayered data to get a properly
  192. whitebalanced picture. If the camera already uses whitebalance gain in the
  193. hardware then those should be stored in the analogbalance metadata instead and
  194. set using the :ref:`libdng_set_analog_balance <set_analog_balance>` function.
  195. In most cases the gain for the green channel will be 1.0f and the red and blue
  196. gains will be adjusted around that. It is important to have the right
  197. analogbalance and neutral whitebalance in the metadata to make the DNG processing
  198. software create the right color matrix from the two provided whitepoints in the
  199. metadata.
  200. The neutral whitepoint defaults to 1.0, 1.0, 1.0.
  201. .. code-block:: c
  202. libdng_info dng = {0};
  203. libdng_new(&dng);
  204. libdng_set_neutral(&dng, 1.2f, 1.0f, 1.6f);
  205. libdng_free(&dng);
  206. Affected tags:
  207. - TIFFTAG_ASSHOTNEUTRAL
  208. .. _set_analog_balance:
  209. int libdng_set_analog_balance(libdng_info \*dng, float r, float g, float b)
  210. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  211. Set the ADC gain used to whitebalance the sensor readings before they were
  212. digitized in the RAW file. This will be used to normalize the raw data again
  213. before using the rest of the color pipeline. Most sensors will not whitebalance
  214. the raw bayer data but some sensors in 8-bit CFA output might do this to optimize
  215. the use of the available bits a bit more.
  216. For the whitebalance after the capture of the raw data the
  217. :ref:`libdng_set_neutral <set_neutral>` function should be used to store the
  218. measured whitebalance.
  219. .. code-block:: c
  220. libdng_info dng = {0};
  221. libdng_new(&dng);
  222. libdng_set_analog_balance(&dng, 1.2f, 1.0f, 1.6f);
  223. libdng_free(&dng);
  224. Affected tags:
  225. - TIFFTAG_ANALOGBALANCE
  226. .. _load_calibration_file:
  227. int libdng_load_calibration_file(libdng_info \*dng, const char \*path)
  228. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  229. Load the calibration metadata from another TIFF file and append it to the final
  230. DNG output. This is used to load all the calibration data from a :code:`.dcp`
  231. file which is a "DNG Color Profile". These files are generated using profiling
  232. software and contain the colormatrices, calibration curves and LUTs to transform
  233. the raw data into a calibrated picture. The exact list of TIFF tags that are
  234. loaded with this command is the list below:
  235. .. code-block:: c
  236. libdng_info dng = {0};
  237. libdng_new(&dng);
  238. libdng_load_calibration_file(&dng, "/tmp/d3300.dcp");
  239. libdng_free(&dng);
  240. Affected tags:
  241. - TIFFTAG_COLOR_MATRIX_1
  242. - TIFFTAG_COLOR_MATRIX_2
  243. - TIFFTAG_FORWARD_MATRIX_1
  244. - TIFFTAG_FORWARD_MATRIX_2
  245. - TIFFTAG_CALIBRATION_ILLUMINANT_1
  246. - TIFFTAG_CALIBRATION_ILLUMINANT_2
  247. - TIFFTAG_PROFILE_TONE_CURVE
  248. - TIFFTAG_PROFILE_HUE_SAT_MAP_DIMS
  249. - TIFFTAG_PROFILE_HUE_SAT_MAP_DATA_1
  250. - TIFFTAG_PROFILE_HUE_SAT_MAP_DATA_2
  251. .. _set_exposure_program:
  252. int libdng_set_exposure_program(libdng_info \*dng, uint16_t mode)
  253. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  254. Sets the exposure program used to take the picture. The intended values here
  255. are the :code:`LIBDNG_EXPOSUREPROGRAM_` constants which maps to the numbers in
  256. the DNG specification for the exposure programs:
  257. .. code-block:: c
  258. #define LIBDNG_EXPOSUREPROGRAM_UNDEFINED 0
  259. #define LIBDNG_EXPOSUREPROGRAM_MANUAL 1
  260. #define LIBDNG_EXPOSUREPROGRAM_NORMAL 2
  261. #define LIBDNG_EXPOSUREPROGRAM_APERTURE_PRIORITY 3
  262. #define LIBDNG_EXPOSUREPROGRAM_SHUTTER_PRIORITY 4
  263. #define LIBDNG_EXPOSUREPROGRAM_CREATIVE 5
  264. #define LIBDNG_EXPOSUREPROGRAM_ACTION 6
  265. #define LIBDNG_EXPOSUREPROGRAM_PORTRAIT 7
  266. #define LIBDNG_EXPOSUREPROGRAM_LANDSCAPE 8
  267. example use:
  268. .. code-block:: c
  269. libdng_info dng = {0};
  270. libdng_new(&dng);
  271. libdng_set_exposure_program(&dng, LIBDNG_EXPOSUREPROGRAM_APERTURE_PRIORITY);
  272. libdng_free(&dng);
  273. Affected tags:
  274. - EXIFTAG_EXPOSUREPROGRAM
  275. .. _write:
  276. int libdng_write(libdng_info \*dng, const char \*path, unsigned int width, unsigned int height, const uint8_t \*data, size_t length)
  277. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  278. This is the function that takes the raw data and stores it into a DNG file together with all the metadata that has
  279. been defined before this.
  280. .. code-block:: c
  281. // This size is only valid for 8-bit pictures.
  282. size_t src_size = 1280 * 720;
  283. uint8_t *data = malloc(src_size);
  284. // At this point the raw data should be _in_ data.
  285. libdng_info dng = {0};
  286. libdng_new(&dng);
  287. if(!libdng_write(&dng, "/tmp/out.dng", 1280, 720, data, src_size)) {
  288. fprintf(stderr, "Could not write DNG\n");
  289. // handle errors here
  290. }
  291. free(data);
  292. libdng_free(&dng);