tools/PPM_to_PATT/src/main.cpp
Go to the documentation of this file.
1 //
2 //
3 // This tool will read a portable pixmap file (binary only) and save
4 // it as an ARToolKit pattern file.
5 //
6 // Usage: PPM_to_PATT [infilename] [outfilename]
7 //
8 // Author: Antonio Bleile, Seac02 S.r.l
9 //
10 
11 #include <stdio.h>
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <memory.h>
15 
16 
17 /* skip comments starting wit # */
18 
19 void
20 skipComments(FILE* fp, char *word)
21 {
22  char dummyChar;
23 
24  while( true ){
25  if( fscanf(fp, "%s", word) ){
26  if (word[0]=='#'){
27  // line is a comment, eat up all chars till the end of line
28 
29  while( fscanf( fp, "%c", &dummyChar ) && dummyChar != '\n' )
30  ;
31  }else
32  return; // line did not start with a comment
33  }else
34  return; // eof reached
35  }
36 }
37 
38 /* read a ppm P6 (binary) image */
39 bool
40 readPPM( const char *fileName, unsigned char **data, int *width, int *height )
41 {
42  int n, dummyInt;
43  char word[1024];
44  char cP, c6;
45  FILE* fp = fopen(fileName, "rb");
46 
47  if(!fp){
48  printf( "could not open %s for reading\n", fileName );
49  return false;
50  }
51 
52  // read magic number P6
53  cP = fgetc( fp );
54  c6 = fgetc( fp );
55 
56  if( cP != 'P' || c6 != '6'){
57  printf( "%s is not a ppm P6 file!\n", fileName );
58  return false;
59  }
60 
61  // skip comments
62  skipComments( fp, word );
63 
64  // read width and height
65  sscanf( word, "%d", width );
66  fscanf( fp, "%d", height );
67 
68  // skip comments
69  skipComments( fp, word );
70 
71  //read number of colors, almost always 255, ignored!
72  sscanf( word, "%d", &dummyInt );
73  fgetc( fp ); // eat up newline
74 
75  int chunkSize = (*width)*(*height)*3*sizeof( unsigned char );
76  if( *data == NULL )
77  *data = new unsigned char[(*width)*(*height)*3];
78 
79  // read actual image data
80  n = fread( *data, 1, chunkSize, fp );
81 
82  if( n != chunkSize ){
83  printf( "read %d bytes, expected %d bytes, file truncated?\n", n,
84 chunkSize );
85  return false;
86  }
87 
88  fclose( fp );
89 
90  return true;
91 }
92 
93 /* rotate left the data */
94 void
95 rotate( unsigned char *data, int width, int height )
96 {
97  // rotate left
98  int i, j;
99  unsigned char *tmp = new unsigned char[width*height*3];
100 
101  for( i = 0; i < width; ++i )
102  for( j = 0; j < height; ++j ){
103  tmp[((width-i-1)*height+j)*3] = data[(j*width+i)*3];
104  tmp[((width-i-1)*height+j)*3+1] = data[(j*width+i)*3+1];
105  tmp[((width-i-1)*height+j)*3+2] = data[(j*width+i)*3+2];
106  }
107 
108  memcpy(data, tmp, width*height*3*sizeof(unsigned char));
109 
110  delete tmp;
111 }
112 
113 /* write artoolkit marker pattern */
114 bool
115 writeMarkerFile(const char *fileName, unsigned char *data, int width, int
116 height)
117 {
118  FILE *fp;
119  int i, j, y, x, tmp;
120  unsigned char *src;
121 
122  fp = fopen( fileName, "w" );
123  if( fp == NULL ) return false;
124 
125  src = new unsigned char[width*height*3];
126  memcpy( src, data, width*height*3*sizeof(unsigned char));
127 
128  for( i = 0; i < 4; i++ ) {
129  for( j = 0; j < 3; j++ ) {
130  for( y = 0; y < height; y++ ) {
131  for( x = 0; x < width; x++ ) {
132  fprintf( fp, "%4d", src[(y*width+x)*3+j] );
133  }
134  fprintf(fp, "\n");
135  }
136  }
137  fprintf(fp, "\n");
138 
139  // rotate left pattern
140  rotate( src, width, height );
141 
142  // swap width and height
143  tmp = width;
144  width = height;
145  height = tmp;
146  }
147 
148  fclose( fp );
149  delete src;
150 
151  return true;
152 }
153 
154 /* convert RGB to BGR */
155 void
156 convert2BGR( unsigned char *data, int width, int height )
157 {
158  int i, j;
159  unsigned char tmp;
160 
161  // just swap R and B
162 
163  for( i = 0; i < width; ++i )
164  for( j = 0; j < height; ++j ){
165  tmp = data[(j*width+i)*3];
166  data[(j*width+i)*3] = data[(j*width+i)*3+2];
167  data[(j*width+i)*3+2] = tmp;
168  }
169 }
170 
171 int
172 main(int argc, char** argv)
173 {
174  unsigned char *ppmData = NULL;
175  int width = 0, height = 0;
176 
177  if(argc<3)
178  {
179  printf("ERROR: too few parameters\n");
180  printf("Usage: %s ppm_infilename pattern_outfilename\n", argv[0] );
181  printf("Example: %s patt_custom.ppm patt.custom\n", argv[0] );
182  return -1;
183  }
184 
185  const char *inName = argv[1],
186  *outName = argv[2];
187 
188 
189  if( !readPPM(inName, &ppmData, &width, &height) )
190  return -1;
191 
192  // not sure about this!
193  convert2BGR( ppmData, width, height );
194 
195  if( ppmData )
196  writeMarkerFile( outName, ppmData, width, height );
197 
198  return 0;
199 }
void rotate(unsigned char *data, int width, int height)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void convert2BGR(unsigned char *data, int width, int height)
TFSIMD_FORCE_INLINE const tfScalar & x() const
int main(int argc, char **argv)
bool writeMarkerFile(const char *fileName, unsigned char *data, int width, int height)
void skipComments(FILE *fp, char *word)
#define NULL
Definition: PocketKnife.h:38
bool readPPM(const char *fileName, unsigned char **data, int *width, int *height)


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33