makedng.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 width Source data width\n");
  15. fprintf(stderr, " -h, --height height Source data height\n");
  16. fprintf(stderr, " -p, --pixfmt fmt Source data pixelformat\n");
  17. fprintf(stderr, " -m, --model make,model Make and model, comma seperated\n");
  18. fprintf(stderr, " -s, --software software Software name\n");
  19. fprintf(stderr, " -o, --orientation orientation Orientation number [0-9]\n");
  20. fprintf(stderr, " -c, --calibration dcp Append calibration data from .dcp file\n");
  21. fprintf(stderr, " -n, --neutral r,g,b Set the whitepoint as 3 comma seperated floats\n");
  22. fprintf(stderr, " -b, --balance r,g,b Set sensor analog gain as 3 comma seperated floats\n");
  23. fprintf(stderr, " -e, --program program Set the exposure program in EXIF, 0-8\n");
  24. fprintf(stderr, " -t, --exposure seconds Set the exposure time in seconds\n");
  25. fprintf(stderr, " -i, --iso speed Set the ISO speed rating\n");
  26. fprintf(stderr, " -f, --fnumber fnumber Set the aperture as f/value\n");
  27. fprintf(stderr, " -l, --focal-length length,crop Set the aperture as f/value\n");
  28. }
  29. int
  30. main(int argc, char *argv[])
  31. {
  32. int c;
  33. char *end;
  34. long val;
  35. libdng_init();
  36. libdng_info info = {0};
  37. libdng_new(&info);
  38. unsigned int width = 0;
  39. unsigned int height = 0;
  40. char *pixelfmt = NULL;
  41. char *model = NULL;
  42. char *software = NULL;
  43. char *calibration = NULL;
  44. uint16_t orientation = 0;
  45. float neutral[] = {1.0f, 1.0f, 1.0f};
  46. float balance[] = {1.0f, 1.0f, 1.0f};
  47. uint16_t exposure_program = 0;
  48. float exposure_time = 0;
  49. uint32_t iso = 0;
  50. float fnumber = 0.0f;
  51. float focal_length = 0.0f;
  52. float crop_factor = 1.0f;
  53. static struct option long_options[] = {
  54. {"width", required_argument, NULL, 'w'},
  55. {"height", required_argument, NULL, 'h'},
  56. {"orientation", required_argument, NULL, 'o'},
  57. {"pixfmt", required_argument, NULL, 'p'},
  58. {"model", required_argument, NULL, 'm'},
  59. {"software", required_argument, NULL, 's'},
  60. {"calibration", required_argument, NULL, 'c'},
  61. {"neutral", required_argument, NULL, 'n'},
  62. {"balance", required_argument, NULL, 'b'},
  63. {"program", required_argument, NULL, 'e'},
  64. {"exposure", required_argument, NULL, 't'},
  65. {"iso", required_argument, NULL, 'i'},
  66. {"fnumber", required_argument, NULL, 'f'},
  67. {"focal-length", required_argument, NULL, 'l'},
  68. {"help", no_argument, NULL, 'H'},
  69. };
  70. int option_index = 0;
  71. while ((c = getopt_long(argc, argv, "w:h:p:o:m:s:c:n:b:e:t:i:f:l:", long_options, &option_index)) != -1) {
  72. switch (c) {
  73. case 'w':
  74. val = strtol(optarg, &end, 10);
  75. width = (unsigned int) val;
  76. break;
  77. case 'h':
  78. val = strtol(optarg, &end, 10);
  79. height = (unsigned int) val;
  80. break;
  81. case 'o':
  82. val = strtol(optarg, &end, 10);
  83. if (val < 1 || val > 8) {
  84. fprintf(stderr, "Orientation out of range\n");
  85. return 1;
  86. }
  87. orientation = (uint16_t) val;
  88. break;
  89. case 'p':
  90. pixelfmt = optarg;
  91. break;
  92. case 'm':
  93. model = optarg;
  94. break;
  95. case 's':
  96. software = optarg;
  97. break;
  98. case 'c':
  99. calibration = optarg;
  100. break;
  101. case 'n':
  102. val = sscanf(optarg, "%f,%f,%f", &neutral[0], &neutral[1], &neutral[2]);
  103. if (val != 3) {
  104. fprintf(stderr, "Invalid format for -n\n");
  105. return 1;
  106. }
  107. break;
  108. case 'b':
  109. val = sscanf(optarg, "%f,%f,%f", &balance[0], &balance[1], &balance[2]);
  110. if (val != 3) {
  111. fprintf(stderr, "Invalid format for -b\n");
  112. return 1;
  113. }
  114. break;
  115. case 'e':
  116. val = strtol(optarg, &end, 10);
  117. exposure_program = (uint16_t) val;
  118. break;
  119. case 't':
  120. exposure_time = strtof(optarg, &end);
  121. break;
  122. case 'i':
  123. val = strtol(optarg, &end, 10);
  124. iso = (uint32_t) val;
  125. break;
  126. case 'f':
  127. fnumber = strtof(optarg, &end);
  128. break;
  129. case 'l':
  130. val = sscanf(optarg, "%f,%f", &focal_length, &crop_factor);
  131. if (val != 2 && val != 1) {
  132. fprintf(stderr, "Invalid format for -l. Specify -l $length,$cropfactor or -l $length\n");
  133. return 1;
  134. }
  135. break;
  136. case 'H':
  137. usage(argv[0]);
  138. return 0;
  139. case '?':
  140. if (optopt == 'd' || optopt == 'l') {
  141. fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  142. } else if (isprint(optopt)) {
  143. fprintf(stderr, "Unknown option '-%c'\n", optopt);
  144. } else {
  145. fprintf(stderr, "Unknown option character x%x\n", optopt);
  146. }
  147. return 1;
  148. default:
  149. return 1;
  150. }
  151. }
  152. if (argc - optind < 2) {
  153. fprintf(stderr, "Missing required argument\n");
  154. usage(argv[0]);
  155. return 1;
  156. }
  157. if (width == 0) {
  158. fprintf(stderr, "The width argument is required\n");
  159. usage(argv[0]);
  160. return 1;
  161. }
  162. if (pixelfmt == NULL) {
  163. fprintf(stderr, "The pixel format argument is required\n");
  164. usage(argv[0]);
  165. return 1;
  166. }
  167. if (!libdng_set_mode_from_name(&info, pixelfmt)) {
  168. fprintf(stderr, "Invalid pixel format supplied\n");
  169. usage(argv[0]);
  170. return 1;
  171. }
  172. if (model != NULL) {
  173. char *make = model;
  174. for (size_t i = 0; i < strlen(model); i++) {
  175. if (model[i] == ',') {
  176. model[i] = '\0';
  177. model = &model[i + 1];
  178. }
  179. }
  180. printf("Make: '%s'\n", make);
  181. printf("Model: '%s'\n", model);
  182. libdng_set_make_model(&info, make, model);
  183. }
  184. if (software != NULL) {
  185. libdng_set_software(&info, software);
  186. }
  187. if (orientation > 0) {
  188. libdng_set_orientation(&info, orientation);
  189. }
  190. if (calibration != NULL) {
  191. libdng_load_calibration_file(&info, calibration);
  192. }
  193. libdng_set_neutral(&info, neutral[0], neutral[1], neutral[2]);
  194. libdng_set_analog_balance(&info, balance[0], balance[1], balance[2]);
  195. libdng_set_exposure_program(&info, exposure_program);
  196. if (exposure_time > 0) {
  197. libdng_set_exposure_time(&info, exposure_time);
  198. }
  199. if (iso > 0) {
  200. libdng_set_iso(&info, iso);
  201. }
  202. if (fnumber > 0) {
  203. libdng_set_fnumber(&info, fnumber);
  204. }
  205. if (focal_length > 0) {
  206. libdng_set_focal_length(&info, focal_length, crop_factor);
  207. }
  208. printf("Reading %s...\n", argv[optind]);
  209. FILE *src = fopen(argv[optind], "r");
  210. if (src == NULL) {
  211. fprintf(stderr, "Can't open source file: %s\n", strerror(errno));
  212. return 1;
  213. }
  214. fseek(src, 0L, SEEK_END);
  215. long src_size = ftell(src);
  216. rewind(src);
  217. uint8_t *data = malloc(src_size);
  218. fread(data, src_size, 1, src);
  219. fclose(src);
  220. printf("Writing %s...\n", argv[optind + 1]);
  221. if (!libdng_write(&info, argv[optind + 1], width, height, data, src_size)) {
  222. fprintf(stderr, "Could not write DNG\n");
  223. return 1;
  224. }
  225. free(data);
  226. libdng_free(&info);
  227. return 0;
  228. }