makedng.c 2.3 KB

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