makedng.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. fprintf(stderr, " -i speed Set the ISO speed rating\n");
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. int c;
  31. char *end;
  32. long val;
  33. libdng_init();
  34. libdng_info info = {0};
  35. libdng_new(&info);
  36. unsigned int width = 0;
  37. unsigned int height = 0;
  38. char *pixelfmt = NULL;
  39. char *model = NULL;
  40. char *software = NULL;
  41. char *calibration = NULL;
  42. uint16_t orientation = 0;
  43. float neutral[] = {1.0f, 1.0f, 1.0f};
  44. float balance[] = {1.0f, 1.0f, 1.0f};
  45. uint16_t exposure_program = 0;
  46. float exposure_time = 0;
  47. uint32_t iso = 0;
  48. while ((c = getopt(argc, argv, "w:h:p:o:m:s:c:n:b:e:t:i:")) != -1) {
  49. switch (c) {
  50. case 'w':
  51. val = strtol(optarg, &end, 10);
  52. width = (unsigned int) val;
  53. break;
  54. case 'h':
  55. val = strtol(optarg, &end, 10);
  56. height = (unsigned int) val;
  57. break;
  58. case 'o':
  59. val = strtol(optarg, &end, 10);
  60. if (val < 1 || val > 8) {
  61. fprintf(stderr, "Orientation out of range\n");
  62. return 1;
  63. }
  64. orientation = (uint16_t) val;
  65. break;
  66. case 'p':
  67. pixelfmt = optarg;
  68. break;
  69. case 'm':
  70. model = optarg;
  71. break;
  72. case 's':
  73. software = optarg;
  74. break;
  75. case 'c':
  76. calibration = optarg;
  77. break;
  78. case 'n':
  79. val = sscanf(optarg, "%f,%f,%f", &neutral[0], &neutral[1], &neutral[2]);
  80. if (val != 3) {
  81. fprintf(stderr, "Invalid format for -n\n");
  82. return 1;
  83. }
  84. break;
  85. case 'b':
  86. val = sscanf(optarg, "%f,%f,%f", &balance[0], &balance[1], &balance[2]);
  87. if (val != 3) {
  88. fprintf(stderr, "Invalid format for -b\n");
  89. return 1;
  90. }
  91. break;
  92. case 'e':
  93. val = strtol(optarg, &end, 10);
  94. exposure_program = (uint16_t) val;
  95. break;
  96. case 't':
  97. exposure_time = strtof(optarg, &end);
  98. break;
  99. case 'i':
  100. val = strtol(optarg, &end, 10);
  101. iso = (uint32_t) val;
  102. break;
  103. case '?':
  104. if (optopt == 'd' || optopt == 'l') {
  105. fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  106. } else if (isprint(optopt)) {
  107. fprintf(stderr, "Unknown option '-%c'\n", optopt);
  108. } else {
  109. fprintf(stderr, "Unknown option character x%x\n", optopt);
  110. }
  111. return 1;
  112. default:
  113. return 1;
  114. }
  115. }
  116. if (argc - optind < 2) {
  117. fprintf(stderr, "Missing required argument\n");
  118. usage(argv[0]);
  119. return 1;
  120. }
  121. if (width == 0) {
  122. fprintf(stderr, "The width argument is required\n");
  123. usage(argv[0]);
  124. return 1;
  125. }
  126. if (pixelfmt == NULL) {
  127. fprintf(stderr, "The pixel format argument is required\n");
  128. usage(argv[0]);
  129. return 1;
  130. }
  131. if (!libdng_set_mode_from_name(&info, pixelfmt)) {
  132. fprintf(stderr, "Invalid pixel format supplied\n");
  133. usage(argv[0]);
  134. return 1;
  135. }
  136. if (model != NULL) {
  137. char *make = model;
  138. for (size_t i = 0; i < strlen(model); i++) {
  139. if (model[i] == ',') {
  140. model[i] = '\0';
  141. model = &model[i + 1];
  142. }
  143. }
  144. printf("Make: '%s'\n", make);
  145. printf("Model: '%s'\n", model);
  146. libdng_set_make_model(&info, make, model);
  147. }
  148. if (software != NULL) {
  149. libdng_set_software(&info, software);
  150. }
  151. if (orientation > 0) {
  152. libdng_set_orientation(&info, orientation);
  153. }
  154. if (calibration != NULL) {
  155. libdng_load_calibration_file(&info, calibration);
  156. }
  157. libdng_set_neutral(&info, neutral[0], neutral[1], neutral[2]);
  158. libdng_set_analog_balance(&info, balance[0], balance[1], balance[2]);
  159. libdng_set_exposure_program(&info, exposure_program);
  160. if (exposure_time > 0) {
  161. libdng_set_exposure_time(&info, exposure_time);
  162. }
  163. if (iso > 0) {
  164. libdng_set_iso(&info, iso);
  165. }
  166. printf("Reading %s...\n", argv[optind]);
  167. FILE *src = fopen(argv[optind], "r");
  168. if (src == NULL) {
  169. fprintf(stderr, "Can't open source file: %s\n", strerror(errno));
  170. return 1;
  171. }
  172. fseek(src, 0L, SEEK_END);
  173. long src_size = ftell(src);
  174. rewind(src);
  175. uint8_t *data = malloc(src_size);
  176. fread(data, src_size, 1, src);
  177. fclose(src);
  178. printf("Writing %s...\n", argv[optind + 1]);
  179. if (!libdng_write(&info, argv[optind + 1], width, height, data, src_size)) {
  180. fprintf(stderr, "Could not write DNG\n");
  181. return 1;
  182. }
  183. free(data);
  184. libdng_free(&info);
  185. return 0;
  186. }