overview.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. Overview
  2. ========
  3. The Adobe DNG standard is the only widely supported open way to exchange raw
  4. camera sensor data between various pieces of software. It uses the TIFF format
  5. to store the sensor data and has a series of custom TIFF tags to describe the
  6. method to convert the raw sensor data to regular RGB color data.
  7. DNG Limitations
  8. ---------------
  9. In order to produce valid dng files from sensor data the only valid options are
  10. a grayscale image produced by a camera with a color filter array (a bayer filter)
  11. or raw data which has been demosaiced into a multi-channel color image.
  12. It is not possible to store YUV encoded image data or any of the UVC camera
  13. supported data formats.
  14. Bit packing of sensor data
  15. --------------------------
  16. Most camera sensors that produce raw data connected to a Linux system will be
  17. connected over the MIPI-CSI bus. The MIPI specifications declare several formats
  18. for transmitting the pixel data. For modes where the camera returns 8-bit data
  19. this is exactly what you expect with one byte mapping to one sample. For 16-bit
  20. data it simply maps to two bytes per sample but a lot of sensor data will be
  21. either 10 or 12 bits per sample.
  22. The packing in the MIPI specification is matching to the packed formats in v4l2:
  23. V4L2_PIX_FMT_SBGGR10P and V4L2_PIX_FMT_SBGGR12P and their other 4 corresponding
  24. CFA orders. These formats pack the 8 most significant bits of the 4 samples as
  25. seperate bytes and then 1 or 2 extra bytes that stores all the "leftover" bits.
  26. .. image:: images/bitpacking.png
  27. In the diagram above the first samples from a camera in BGGR mode is shown. The
  28. first line has alternating blue and red color filters here shown in two shades
  29. of blue and green to differentiate the 4 samples. The semi-transparent bits are
  30. the least significant bits of each sample.
  31. The DNG standard specifies a subset of TIFF image data encodings for storing the
  32. sensor data into the DNG. These formats sadly do not line up with the pixel
  33. format of MIPI sensor data like most cameras use. The libdng code provides some
  34. helper code to convert the 10 and 12-bit MIPI pixel formats to the more linear
  35. format used in the TIFF file format.
  36. Writing a simple DNG file
  37. -------------------------
  38. The minimum code for writing a valid DNG file is the following:
  39. .. code-block:: c
  40. #include <libdng.h>
  41. // Imagine you have real data
  42. uint8_t *data = <from somewhere>;
  43. size_t data_length = 1234;
  44. // Run once to register custom tags in libtiff
  45. libdng_init();
  46. // New image
  47. libdng_info dng = {0};
  48. libdng_new(&dng);
  49. // Set the pixel format to 10-bit RGGB in MIPI format
  50. if (!libdng_set_mode_from_name(&dng, "SRGGB10P")) {
  51. fprintf(stderr, "Invalid pixel format supplied\n");
  52. }
  53. // Call any other libdng_set_* methods here
  54. if (!libdng_write(&dng, "out.dng", width, height, data, data_length)) {
  55. fprintf(stderr, "Could not write DNG\n");
  56. }
  57. libdng_free(&dng);
  58. This writes out the raw sensor data from :code:`*data` to the `out.dng` file
  59. with the right metadata for a RGGB color filter array. It also converts the
  60. 10-bit packed format to the linear format from the TIFF spec.
  61. Operations that change metadata can be executed in any order between the
  62. :code:`libdng_new` and :code:`libdng_write` command. The ony required metadata
  63. is one of the :code:`libdng_set_mode_*` calls to bring all the pixelformat
  64. metadata in order.
  65. Setting the mode
  66. ----------------
  67. The only required metadata operation is setting the pixelformat. There are
  68. multiple ways of specifying the format:
  69. * With :code:`libdng_set_mode_from_name(&dng, "name")`:
  70. This is setting the format from one of the named modes. The accepted named
  71. modes here are the 4 character fourcc codes defined by V4L2 and also the
  72. more intuitively named V4L2 constants without the :code:`V4L2_PIX_FMT_`
  73. prefix in front.
  74. * With :code:`libdng_set_mode_from_pixfmt(&dng, pixfmt)`:
  75. This does the same thing but instead of passing the format as string you
  76. pass the value of the :code:`V4L2_PIX_FMT_` constants as an integer. This is
  77. easier for integrating the DNG writing with existing V4L2 code.
  78. If the format is one of the packed formats the :code:`libdng_write` method will
  79. automatically convert the format to fit in a TIFF container.