makedng.c 3.6 KB

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