main.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //
00004 //  This tool will read ARToolKit pattern files (with variable size) and save
00005 //  them as Portable Pixmap (.ppm) files.
00006 //
00007 //  Usage: PATT_to_PPM [infilename] [outfilename] [width] [height]
00008 //
00009 //
00010 //  Thanks to Antonio Bleile for contributing the PPM writer code
00011 // 
00012 
00013 
00014 #include <stdio.h>
00015 #include <assert.h>
00016 #include <stdlib.h>
00017 
00018 
00019 
00020 char
00021 skipSpaces(FILE* fp)
00022 {
00023         char ch = ' ';
00024 
00025         while( ch==' ' && !feof(fp))
00026                 ch = fgetc(fp);
00027 
00028         return ch;
00029 }
00030 
00031 
00032 bool
00033 readNumber(FILE* fp, char* buffer, int maxLen)
00034 {
00035         char ch = 0;
00036         int i=0;
00037 
00038         do {
00039                 ch = fgetc(fp);
00040                 if(ch>='0' && ch<='9')
00041                         buffer[i++] = ch;
00042         } while(!feof(fp) && i<maxLen-1 && ch!=' ');
00043 
00044 
00045         buffer[i++] = 0;
00046         return i<maxLen && !feof(fp);
00047 }
00048 
00049 
00050 bool
00051 writePPM( const char *fileName, unsigned char *data, int width, int height )
00052 {
00053         FILE *file = fopen( fileName, "wb" );
00054         if( !file ){
00055                 printf( "Could not open file %s for writing\n", fileName );
00056                 return false;
00057         }
00058 
00059         // error checking omitted
00060         fprintf( file, "P6\n# generated by patt_to_ppm\n%d %d\n255\n", width, height );
00061         fwrite( data, width*height*3, 1, file );
00062 
00063         fclose( file );
00064 
00065         return true;
00066 }
00067 
00068 
00069 unsigned char *
00070 readMarkerFile(const char* nFileName, int nMarkerWidth, int nMarkerHeight)
00071 {
00072         FILE* fp = fopen(nFileName, "r");
00073 
00074         if(!fp)
00075                 return NULL;
00076 
00077         const int strMax = 256, numPix = nMarkerWidth*nMarkerHeight;
00078         unsigned char* pixels24 = new unsigned char[numPix*3];
00079         char str[strMax+1];
00080         int x,y,channel;
00081 
00082         for(channel=0; channel<3; channel++)
00083                 for(y=0; y<nMarkerHeight; y++)
00084                 {
00085                         for(x=0; x<nMarkerWidth; x++)
00086                         {
00087                                 str[0] = skipSpaces(fp);
00088                                 readNumber(fp, str+1, strMax);
00089                                 int value = atoi(str);
00090                                 int offset = 3*(x + y*nMarkerWidth) + channel;
00091                                 assert(offset < numPix*3);
00092                                 pixels24[offset] = value;
00093                         }
00094                 }
00095 
00096 
00097         fclose(fp);
00098 
00099         return pixels24;
00100 }
00101 
00102 
00103 int main(int argc, char** argv)
00104 {
00105         unsigned char *patternImage;
00106         int width = 16, height = 16;
00107 
00108         if(argc<3)
00109         {
00110                 printf("ERROR: to few parameters\n");
00111                 printf("Usage: %s infilename outfilename [width=16] [height=16]\n", argv[0] );
00112                 printf("Example: %s patt.hiro patt_hiro.ppm 32 32\n", argv[0] );
00113                 return -1;
00114         }
00115 
00116         const char      *inName = argv[1],
00117                                 *outName = argv[2];
00118 
00119         if( argc >= 4 )
00120                 width = atoi( argv[3] );
00121         if( argc >= 5 )
00122                 height = atoi( argv[4] );
00123 
00124         patternImage = readMarkerFile(inName, width,height);
00125 
00126         if( patternImage )
00127                 writePPM( outName, patternImage, width, height );
00128 
00129         return 0;
00130 }


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun May 29 2016 02:50:12