00001
00002
00003
00004
00005 #define FCAM_VERSION "1.1.0"
00006
00007 #include <FCam/FCam.h>
00008 #include <stdio.h>
00009 #include <unistd.h>
00010
00011 void usage() {
00012 printf("fcamDngUtil\n"
00013 " (FCam version " FCAM_VERSION ")\n"
00014 " A simply utility to manipulate DNGs saved by programs using\n"
00015 " the FCam API.\n\n"
00016
00017 "Usage: fcamDngUtil [FLAGS] INPUT [OUTPUT].\n"
00018 " With no output name given, adds .up. before the .dng extension to\n"
00019 " the input to create the output name for DNG output, and uses the\n"
00020 " input file name with the .jpg extension for JPEG.\n"
00021 "Flags:\n"
00022 " -i Dump out information about the DNG contents\n"
00023 " -u Write a new DNG with metadata updated to the latest version.\n"
00024 " -j Write a JPEG file created from the input DNG.\n"
00025 " If -u is also specified, only -j takes effect.\n"
00026 " -c Specify a different contrast amount for JPEG conversion\n"
00027 " (default = 50, range 0-100, larger numbers increase contrast.)"
00028 " -b Specify a different black level for JPEG conversion (default = 25).\n"
00029 " -g Specify a different gamma value for JPEG conversion (default = 2.2).\n"
00030 " -q Specify JPEG quality (default = 80, range 0-100).\n"
00031 " -n Turn off hot pixel suppression.\n"
00032 "\n");
00033 exit(1);
00034 }
00035
00036 int main(int argc, char **argv) {
00037 if (argc < 2) {
00038 usage();
00039 }
00040
00041 bool dumpFrame = false;
00042 bool writeDNG = false;
00043 bool writeJPEG = false;
00044
00045 bool denoise = true;
00046 int contrast = 50;
00047 int blackLevel = 25;
00048 float gamma = 2.2f;
00049 int quality = 80;
00050
00051 const char *options="iujc:b:g:nq:";
00052
00053 int argChar;
00054 while ( (argChar = getopt(argc,argv,options)) != -1) {
00055 switch (argChar) {
00056 case 'i':
00057 dumpFrame = true;
00058 break;
00059 case 'u':
00060 writeDNG = true;
00061 break;
00062 case 'j':
00063 writeJPEG = true;
00064 break;
00065 case 'c':
00066 sscanf(optarg,"%d", &contrast);
00067 break;
00068 case 'b':
00069 sscanf(optarg,"%d", &blackLevel);
00070 break;
00071 case 'g':
00072 sscanf(optarg,"%f", &gamma);
00073 break;
00074 case 'n':
00075 denoise = false;
00076 break;
00077 case 'q':
00078 sscanf(optarg,"%d", &quality);
00079 break;
00080 case '?':
00081 usage();
00082 break;
00083 }
00084 }
00085
00086 if (optind == argc) {
00087 printf("!! Error: No input file given!\n\n");
00088 usage();
00089 }
00090
00091 std::string inputFile(argv[optind]);
00092
00093 std::string outputFile;
00094 if (optind < argc -1 ) {
00095 outputFile = std::string(argv[optind+1]);
00096 } else {
00097 if (writeJPEG) {
00098 outputFile = inputFile.substr(0,inputFile.rfind(".dng"));
00099 outputFile.append(".jpg");
00100 } else {
00101 outputFile = inputFile.substr(0,inputFile.rfind(".dng"));
00102 outputFile.append(".up.dng");
00103 }
00104 }
00105
00106 if (!dumpFrame && !writeJPEG && !writeDNG) {
00107 printf("!! Error: Nothing to do\n\n");
00108 usage();
00109 }
00110
00111 FCam::Event e;
00112
00113 printf(" Reading input DNG %s\n", inputFile.c_str());
00114
00115 FCam::DNGFrame f = FCam::loadDNG(inputFile);
00116
00117 if (FCam::_eventQueue.size() > 0) {
00118 printf(" Events reported while loading DNG:\n");
00119 while (getNextEvent(&e)) {
00120 switch (e.type) {
00121 case FCam::Event::Error:
00122 printf(" Error code: %d. Description: %s\n", e.data, e.description.c_str());
00123 break;
00124 case FCam::Event::Warning:
00125 printf(" Warning code: %d. Description: %s\n", e.data, e.description.c_str());
00126 break;
00127 default:
00128 printf(" Event type %d, code: %d. Description: %s\n", e.type, e.data, e.description.c_str());
00129 break;
00130 }
00131 }
00132 }
00133
00134 if (!f.valid()) {
00135 printf("!! Error: Unable to read input file %s\n", inputFile.c_str());
00136 exit(1);
00137 }
00138
00139 if (dumpFrame) {
00140 f.debug(inputFile.c_str());
00141 }
00142
00143 if (writeJPEG) {
00144 printf(" Demosaicing with contrast %d, black level %d, gamma %f %s\n", contrast, blackLevel, gamma, denoise ? "":"(Denoising off)");
00145 FCam::Image jpegImg = FCam::demosaic(f, contrast, denoise, blackLevel, gamma);
00146 if (FCam::_eventQueue.size() > 0) {
00147 printf("Events reported while demosaicing DNG:\n");
00148 while (getNextEvent(&e)) {
00149 switch (e.type) {
00150 case FCam::Event::Error:
00151 printf(" Error code: %d. Description: %s\n", e.data, e.description.c_str());
00152 break;
00153 case FCam::Event::Warning:
00154 printf(" Warning code: %d. Description: %s\n", e.data, e.description.c_str());
00155 break;
00156 default:
00157 printf(" Event type %d, code: %d. Description: %s\n", e.type, e.data, e.description.c_str());
00158 break;
00159 }
00160 }
00161 }
00162 if (!jpegImg.valid()) {
00163 printf("!! Error: Unable to demosaic input file %s\n", inputFile.c_str());
00164 exit(1);
00165 }
00166 printf(" Writing to %s, quality %d\n", outputFile.c_str(), quality);
00167 saveJPEG(jpegImg, outputFile, quality);
00168
00169 if (FCam::_eventQueue.size() > 0) {
00170 printf("Events reported while saving JPEG:\n");
00171 while (getNextEvent(&e)) {
00172 switch (e.type) {
00173 case FCam::Event::Error:
00174 printf(" Error code: %d. Description: %s\n", e.data, e.description.c_str());
00175 break;
00176 case FCam::Event::Warning:
00177 printf(" Warning code: %d. Description: %s\n", e.data, e.description.c_str());
00178 break;
00179 default:
00180 printf(" Event type %d, code: %d. Description: %s\n", e.type, e.data, e.description.c_str());
00181 break;
00182 }
00183 }
00184 }
00185
00186 } else if (writeDNG) {
00187 printf(" Writing updated DNG to %s\n", outputFile.c_str());
00188 FCam::saveDNG(f, outputFile);
00189
00190 if (FCam::_eventQueue.size() > 0) {
00191 printf(" Events reported while saving to %s:\n", outputFile.c_str());
00192 while (getNextEvent(&e)) {
00193 switch (e.type) {
00194 case FCam::Event::Error:
00195 printf(" Error code: %d. Description: %s\n", e.data, e.description.c_str());
00196 break;
00197 case FCam::Event::Warning:
00198 printf(" Warning code: %d. Description: %s\n", e.data, e.description.c_str());
00199 break;
00200 default:
00201 printf(" Event type %d, code: %d. Description: %s\n", e.type, e.data, e.description.c_str());
00202 break;
00203 }
00204 }
00205 }
00206 }
00207 printf(" Done!\n");
00208 return 0;
00209 }