makedng.c 4.6 KB

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