makedng.c 4.3 KB

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