main.cpp
Go to the documentation of this file.
00001 //
00002 //
00003 //  This tool will read a portable pixmap file (binary only) and save
00004 //  it as an ARToolKit pattern file.
00005 //
00006 //  Usage: PPM_to_PATT [infilename] [outfilename]
00007 //
00008 //  Author: Antonio Bleile, Seac02 S.r.l
00009 //
00010 
00011 #include <stdio.h>
00012 #include <assert.h>
00013 #include <stdlib.h>
00014 #include <memory.h>
00015 
00016 
00017 /* skip comments starting wit # */
00018 
00019 void
00020 skipComments(FILE* fp, char *word)
00021 {
00022     char dummyChar;
00023 
00024     while( true ){
00025         if( fscanf(fp, "%s", word) ){
00026             if (word[0]=='#'){
00027                 // line is a comment, eat up all chars till the end of line
00028 
00029                 while( fscanf( fp, "%c", &dummyChar ) && dummyChar != '\n' )
00030                     ;
00031             }else
00032                 return; // line did not start with a comment
00033         }else
00034             return; // eof reached
00035     }
00036 }
00037 
00038 /* read a ppm P6 (binary) image */
00039 bool
00040 readPPM( const char *fileName, unsigned char **data, int *width, int *height )
00041 {
00042     int n, dummyInt;
00043     char word[1024];
00044     char cP, c6;
00045     FILE* fp = fopen(fileName, "rb");
00046 
00047     if(!fp){
00048         printf( "could not open %s for reading\n", fileName );
00049         return false;
00050     }
00051 
00052     // read magic number P6
00053     cP = fgetc( fp );
00054     c6 = fgetc( fp );
00055 
00056     if( cP != 'P' || c6 != '6'){
00057         printf( "%s is not a ppm P6 file!\n", fileName );
00058         return false;
00059     }
00060 
00061     // skip comments
00062     skipComments( fp, word );
00063 
00064     // read width and height
00065     sscanf( word, "%d", width );
00066     fscanf( fp, "%d", height );
00067 
00068     // skip comments
00069     skipComments( fp, word );
00070 
00071     //read number of colors, almost always 255, ignored!
00072     sscanf( word, "%d", &dummyInt );
00073     fgetc( fp ); // eat up newline
00074 
00075     int chunkSize = (*width)*(*height)*3*sizeof( unsigned char );
00076     if( *data == NULL )
00077         *data = new unsigned char[(*width)*(*height)*3];
00078 
00079     // read actual image data
00080     n = fread( *data, 1, chunkSize, fp );
00081 
00082     if( n != chunkSize ){
00083         printf( "read %d bytes, expected %d bytes, file truncated?\n", n,
00084 chunkSize );
00085         return false;
00086     }
00087 
00088     fclose( fp );
00089 
00090     return true;
00091 }
00092 
00093 /* rotate left the data */
00094 void
00095 rotate( unsigned char *data, int width, int height )
00096 {
00097     // rotate left
00098     int i, j;
00099     unsigned char *tmp = new unsigned char[width*height*3];
00100 
00101     for( i = 0; i < width; ++i )
00102         for( j = 0; j < height; ++j ){
00103             tmp[((width-i-1)*height+j)*3]   = data[(j*width+i)*3];
00104             tmp[((width-i-1)*height+j)*3+1] = data[(j*width+i)*3+1];
00105             tmp[((width-i-1)*height+j)*3+2] = data[(j*width+i)*3+2];
00106         }
00107 
00108     memcpy(data, tmp, width*height*3*sizeof(unsigned char));
00109 
00110     delete tmp;
00111 }
00112 
00113 /* write artoolkit marker pattern */
00114 bool
00115 writeMarkerFile(const char *fileName, unsigned char *data, int width, int
00116 height)
00117 {
00118     FILE *fp;
00119     int i, j, y, x, tmp;
00120     unsigned char *src;
00121 
00122     fp = fopen( fileName, "w" );
00123     if( fp == NULL ) return false;
00124 
00125     src = new unsigned char[width*height*3];
00126     memcpy( src, data, width*height*3*sizeof(unsigned char));
00127 
00128     for( i = 0; i < 4; i++ ) {
00129         for( j = 0; j < 3; j++ ) {
00130             for( y = 0; y < height; y++ ) {
00131                 for( x = 0; x < width; x++ ) {
00132                     fprintf( fp, "%4d", src[(y*width+x)*3+j] );
00133                 }
00134                 fprintf(fp, "\n");
00135             }
00136         }
00137         fprintf(fp, "\n");
00138 
00139         // rotate left pattern
00140         rotate( src, width, height );
00141 
00142         // swap width and height
00143         tmp = width;
00144         width = height;
00145         height = tmp;
00146     }
00147 
00148     fclose( fp );
00149     delete src;
00150 
00151     return true;
00152 }
00153 
00154 /* convert RGB to BGR */
00155 void
00156 convert2BGR( unsigned char *data, int width, int height )
00157 {
00158     int i, j;
00159     unsigned char tmp;
00160 
00161     // just swap R and B
00162 
00163     for( i = 0; i < width; ++i )
00164         for( j = 0; j < height; ++j ){
00165             tmp = data[(j*width+i)*3];
00166             data[(j*width+i)*3] = data[(j*width+i)*3+2];
00167             data[(j*width+i)*3+2] = tmp;
00168         }
00169 }
00170 
00171 int
00172 main(int argc, char** argv)
00173 {
00174     unsigned char *ppmData = NULL;
00175     int width = 0, height = 0;
00176 
00177     if(argc<3)
00178     {
00179         printf("ERROR: too few parameters\n");
00180         printf("Usage: %s ppm_infilename pattern_outfilename\n", argv[0] );
00181         printf("Example: %s patt_custom.ppm patt.custom\n", argv[0] );
00182         return -1;
00183     }
00184 
00185     const char    *inName = argv[1],
00186                 *outName = argv[2];
00187 
00188 
00189     if( !readPPM(inName, &ppmData, &width, &height) )
00190         return -1;
00191 
00192     // not sure about this!
00193     convert2BGR( ppmData, width, height );
00194 
00195     if( ppmData )
00196         writeMarkerFile( outName, ppmData, width, height );
00197 
00198     return 0;
00199 }


v4r_artoolkitplus
Author(s): Markus Bader
autogenerated on Wed Aug 26 2015 16:41:53