1 /*******************************************************************************
3 * clr2float_conv - Colorful image to float array converter desktop utility.
5 * Created for Eurobot 2010 competition.
7 * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
9 * !!! @deprecated !!! - the program is useless since maskgen generates pnm files,
10 * which are then loaded directly by rozkuk
12 * Usage: ./clr2float_conv filenames... [-t value]
14 * Names of picture files to convert.
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.
19 * Output: Files with the same names as the input files, but with '.bin' extension.
21 ******************************************************************************/
24 #include <opencv/highgui.h>
25 #include "clr2float.h"
28 /******************************************************************************/
30 //extension of output files
31 #define OUTPUT_EXT "bin"
33 /******************************************************************************/
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;
44 memcpy(dest, filename, (strlen(filename) < destLen ? strlen(filename) : destLen));
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;
50 strcpy(pch+1, ext); // change ext
54 /******************************************************************************/
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;
63 int outputLen, writtenLen;
65 // store output filename to destFilename
66 changeFilenameExt(filename, OUTPUT_EXT, destFilename, sizeof(destFilename));
68 // open input and output files
69 pInput = fopen(filename, "r");
71 fprintf(stderr, "Error: File %s does not exist!\n", filename);
74 /* pOutput = fopen(destFilename, "w");
76 fprintf(stderr, "Error: Cannot write to file %s!\n", destFilename);
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);
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);
95 fprintf(stderr, "File \"%s\" processed, but an error occured when writting the output to \"%s\".\n", filename, destFilename);
97 cvReleaseImage(&clrImage);
103 /******************************************************************************/
105 /** Processes the program arguments and for every input generates converted output. */
106 int main(int argc, char *argv[]) {
108 int thr=128; //some default value of threshold
110 // scan for program arguments
111 while ((opt = getopt(argc, argv, "t:")) != -1) {
114 if(optarg) thr = atoi(optarg); //threshold
118 // "undefined" program arguments = input filenames
119 for(int i = optind; i < argc; i++)
120 processFile(argv[i], thr);