makedng.c 5.6 KB

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