]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/camera/rozkuk/clr2float_conv.cxx
Small edit due to changes in the conversion library.
[eurobot/public.git] / src / camera / rozkuk / clr2float_conv.cxx
1 /*******************************************************************************
2  * 
3  * clr2float_conv - Colorful image to float array converter desktop utility.
4  *
5  * Created for Eurobot 2010 competition.
6  *
7  * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
8  *
9  * Usage: ./clr2float_conv filenames... [-t value]
10  *  filenames:
11  *   Names of picture files to convert.
12  *  -t:
13  *   Threshold. Value, which will be displayed to 0.0, while less values
14  *   will display to negative floats >= -1.0 and greater to positive floats <= 1.0.
15  *
16  * Output: Files with the same names as the input files, but with '.bin' extension.
17  * 
18  ******************************************************************************/
19
20 #include <stdio.h>
21 #include <opencv/highgui.h>
22 #include "clr2float.h"
23 #include <unistd.h>
24
25 /******************************************************************************/
26
27 //extension of output files
28 #define OUTPUT_EXT              "bin"
29
30 /******************************************************************************/
31
32 /** Stores a filename with changed extension to dest.
33  * If filename contains a dot, the string after the last one is replaced by ext.
34  * If no dot is found, the ext is appended to the filename.
35  * @param destLen Max length of the result string.
36  * @return 0 if ok, (!0) in case of an error. */
37 int changeFilenameExt(const char *filename, const char *ext, char *dest, unsigned int destLen) {
38         if(!filename) return 1;
39         if(!dest) return 2;
40
41         memcpy(dest, filename, (strlen(filename) < destLen ? strlen(filename) : destLen));
42         
43         char *pch = strrchr(dest, '.');                                                                                         // search for the last dot
44         if(!pch) pch = dest+strlen(dest);
45         if((unsigned)(pch-dest) > destLen-strlen(ext)-1) pch = dest+destLen-strlen(ext)-1;
46         strcpy(pch, ".");
47         strcpy(pch+1, ext);                                                                                                                                             // change ext
48         return 0;
49 }
50
51 /******************************************************************************/
52
53 /** Processes the given file and stores to a new one. */
54 int processFile(const char *filename, unsigned char threshold) {
55         FILE *pInput, *pOutput;
56         char destFilename[100];
57         IplImage *clrImage = NULL;
58         CvMat *outputMat = NULL;
59         float *output = NULL;
60         int outputLen, writtenLen;
61         
62         // store output filename to destFilename
63   changeFilenameExt(filename, OUTPUT_EXT, destFilename, sizeof(destFilename));
64   
65   // open input and output files
66   pInput = fopen(filename, "r");
67   if(!pInput) {
68         fprintf(stderr, "Error: File %s does not exist!\n", filename);
69         return 1;
70   }
71   pOutput = fopen(destFilename, "w");
72   if(!pOutput) {
73         fprintf(stderr, "Error: Cannot write to file %s!\n", destFilename);
74         fclose(pInput);
75         return 2;
76   }
77   // load input image and generate a float <-1,1> format
78   clrImage = cvLoadImage(filename);
79   if(!clrImage) return 1;
80   if(!(outputLen=clr2float(clrImage, &outputMat, threshold, NULL))) {
81                 cvReleaseImage(&clrImage);
82                 fclose(pInput);
83                 fclose(pOutput);
84   }
85   // write the data to the file
86         output = outputMat->data.fl;
87   writtenLen = fwrite(output, sizeof(output[0]), outputLen, pOutput);
88   if(writtenLen != outputLen)
89         fprintf(stderr, "File \"%s\" processed, but an error occured when writting the output to \"%s\".\n", filename, destFilename);
90   else
91         fprintf(stderr, "File \"%s\" processed. Output successfully written to \"%s\".\n", filename, destFilename);
92   
93   cvReleaseImage(&clrImage);
94   fclose(pInput);
95   fclose(pOutput);
96   return 0;
97 }
98
99 /******************************************************************************/
100
101 /** Processes the program arguments and for every input generates converted output. */
102 int main(int argc, char *argv[]) {
103         char opt;
104         int thr=130;                                                            //some default value of threshold
105         
106         // scan for program arguments
107   while ((opt = getopt(argc, argv, "t:")) != -1) {
108     switch (opt) {
109     case 't':
110       if(optarg) thr = atoi(optarg);                                                                            //threshold
111       break;
112     }
113   }
114   // "undefined" program arguments = input filenames
115   for(int i = optind; i < argc; i++)
116                 processFile(argv[i], thr);
117   
118         return 0;
119 }
120