makedng.c 7.0 KB

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