makedng.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <getopt.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include "libdng.h"
  8. void
  9. usage(char *name)
  10. {
  11. fprintf(stderr, "Usage: %s -w width -h height -p fmt srcfile dstfile\n", name);
  12. fprintf(stderr, "Convert raw sensor data to DNG\n\n");
  13. fprintf(stderr, "Arguments:\n");
  14. fprintf(stderr, " -w width Source data width\n");
  15. fprintf(stderr, " -h height Source data height\n");
  16. fprintf(stderr, " -p fmt Source data pixelformat\n");
  17. fprintf(stderr, " -m make,model Make and model, comma seperated\n");
  18. fprintf(stderr, " -s software Software name\n");
  19. fprintf(stderr, " -o orientation Orientation number [0-9]\n");
  20. fprintf(stderr, " -c dcp Append calibration data from .dcp file\n");
  21. fprintf(stderr, " -n r,g,b Set the whitepoint as 3 comma seperated floats\n");
  22. fprintf(stderr, " -b r,g,b Set sensor analog gain as 3 comma seperated floats\n");
  23. fprintf(stderr, " -e program Set the exposure program in EXIF, 0-8\n");
  24. fprintf(stderr, " -t seconds Set the exposure time in seconds\n");
  25. }
  26. int
  27. main(int argc, char *argv[])
  28. {
  29. int c;
  30. char *end;
  31. long val;
  32. libdng_init();
  33. libdng_info info = {0};
  34. libdng_new(&info);
  35. unsigned int width = 0;
  36. unsigned int height = 0;
  37. char *pixelfmt = NULL;
  38. char *model = NULL;
  39. char *software = NULL;
  40. char *calibration = NULL;
  41. uint16_t orientation = 0;
  42. float neutral[] = {1.0f, 1.0f, 1.0f};
  43. float balance[] = {1.0f, 1.0f, 1.0f};
  44. uint16_t exposure_program = 0;
  45. float exposure_time = 0;
  46. while ((c = getopt(argc, argv, "w:h:p:o:m:s:c:n:b:e:t:")) != -1) {
  47. switch (c) {
  48. case 'w':
  49. val = strtol(optarg, &end, 10);
  50. width = (unsigned int) val;
  51. break;
  52. case 'h':
  53. val = strtol(optarg, &end, 10);
  54. height = (unsigned int) val;
  55. break;
  56. case 'o':
  57. val = strtol(optarg, &end, 10);
  58. if (val < 1 || val > 8) {
  59. fprintf(stderr, "Orientation out of range\n");
  60. return 1;
  61. }
  62. orientation = (uint16_t) val;
  63. break;
  64. case 'p':
  65. pixelfmt = optarg;
  66. break;
  67. case 'm':
  68. model = optarg;
  69. break;
  70. case 's':
  71. software = optarg;
  72. break;
  73. case 'c':
  74. calibration = optarg;
  75. break;
  76. case 'n':
  77. val = sscanf(optarg, "%f,%f,%f", &neutral[0], &neutral[1], &neutral[2]);
  78. if (val != 3) {
  79. fprintf(stderr, "Invalid format for -n\n");
  80. return 1;
  81. }
  82. break;
  83. case 'b':
  84. val = sscanf(optarg, "%f,%f,%f", &balance[0], &balance[1], &balance[2]);
  85. if (val != 3) {
  86. fprintf(stderr, "Invalid format for -b\n");
  87. return 1;
  88. }
  89. break;
  90. case 'e':
  91. val = strtol(optarg, &end, 10);
  92. exposure_program = (uint16_t) val;
  93. break;
  94. case 't':
  95. exposure_time = strtof(optarg, &end);
  96. break;
  97. case '?':
  98. if (optopt == 'd' || optopt == 'l') {
  99. fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  100. } else if (isprint(optopt)) {
  101. fprintf(stderr, "Unknown option '-%c'\n", optopt);
  102. } else {
  103. fprintf(stderr, "Unknown option character x%x\n", optopt);
  104. }
  105. return 1;
  106. default:
  107. return 1;
  108. }
  109. }
  110. if (argc - optind < 2) {
  111. fprintf(stderr, "Missing required argument\n");
  112. usage(argv[0]);
  113. return 1;
  114. }
  115. if (width == 0) {
  116. fprintf(stderr, "The width argument is required\n");
  117. usage(argv[0]);
  118. return 1;
  119. }
  120. if (pixelfmt == NULL) {
  121. fprintf(stderr, "The pixel format argument is required\n");
  122. usage(argv[0]);
  123. return 1;
  124. }
  125. if (!libdng_set_mode_from_name(&info, pixelfmt)) {
  126. fprintf(stderr, "Invalid pixel format supplied\n");
  127. usage(argv[0]);
  128. return 1;
  129. }
  130. if (model != NULL) {
  131. char *make = model;
  132. for (size_t i = 0; i < strlen(model); i++) {
  133. if (model[i] == ',') {
  134. model[i] = '\0';
  135. model = &model[i + 1];
  136. }
  137. }
  138. printf("Make: '%s'\n", make);
  139. printf("Model: '%s'\n", model);
  140. libdng_set_make_model(&info, make, model);
  141. }
  142. if (software != NULL) {
  143. libdng_set_software(&info, software);
  144. }
  145. if (orientation > 0) {
  146. libdng_set_orientation(&info, orientation);
  147. }
  148. if (calibration != NULL) {
  149. libdng_load_calibration_file(&info, calibration);
  150. }
  151. libdng_set_neutral(&info, neutral[0], neutral[1], neutral[2]);
  152. libdng_set_analog_balance(&info, balance[0], balance[1], balance[2]);
  153. libdng_set_exposure_program(&info, exposure_program);
  154. if (exposure_time > 0) {
  155. libdng_set_exposure_time(&info, exposure_time);
  156. }
  157. printf("Reading %s...\n", argv[optind]);
  158. FILE *src = fopen(argv[optind], "r");
  159. if (src == NULL) {
  160. fprintf(stderr, "Can't open source file: %s\n", strerror(errno));
  161. return 1;
  162. }
  163. fseek(src, 0L, SEEK_END);
  164. long src_size = ftell(src);
  165. rewind(src);
  166. uint8_t *data = malloc(src_size);
  167. fread(data, src_size, 1, src);
  168. fclose(src);
  169. printf("Writing %s...\n", argv[optind + 1]);
  170. if (!libdng_write(&info, argv[optind + 1], width, height, data, src_size)) {
  171. fprintf(stderr, "Could not write DNG\n");
  172. return 1;
  173. }
  174. free(data);
  175. libdng_free(&info);
  176. return 0;
  177. }