makedng.c 2.8 KB

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