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