makedng.c 3.3 KB

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