makedng.c 2.0 KB

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