tools/PATT_to_PPM/src/main.cpp
Go to the documentation of this file.
1 
2 //
3 //
4 // This tool will read ARToolKit pattern files (with variable size) and save
5 // them as Portable Pixmap (.ppm) files.
6 //
7 // Usage: PATT_to_PPM [infilename] [outfilename] [width] [height]
8 //
9 //
10 // Thanks to Antonio Bleile for contributing the PPM writer code
11 //
12 
13 
14 #include <stdio.h>
15 #include <assert.h>
16 #include <stdlib.h>
17 
18 
19 
20 char
21 skipSpaces(FILE* fp)
22 {
23  char ch = ' ';
24 
25  while( ch==' ' && !feof(fp))
26  ch = fgetc(fp);
27 
28  return ch;
29 }
30 
31 
32 bool
33 readNumber(FILE* fp, char* buffer, int maxLen)
34 {
35  char ch = 0;
36  int i=0;
37 
38  do {
39  ch = fgetc(fp);
40  if(ch>='0' && ch<='9')
41  buffer[i++] = ch;
42  } while(!feof(fp) && i<maxLen-1 && ch!=' ');
43 
44 
45  buffer[i++] = 0;
46  return i<maxLen && !feof(fp);
47 }
48 
49 
50 bool
51 writePPM( const char *fileName, unsigned char *data, int width, int height )
52 {
53  FILE *file = fopen( fileName, "wb" );
54  if( !file ){
55  printf( "Could not open file %s for writing\n", fileName );
56  return false;
57  }
58 
59  // error checking omitted
60  fprintf( file, "P6\n# generated by patt_to_ppm\n%d %d\n255\n", width, height );
61  fwrite( data, width*height*3, 1, file );
62 
63  fclose( file );
64 
65  return true;
66 }
67 
68 
69 unsigned char *
70 readMarkerFile(const char* nFileName, int nMarkerWidth, int nMarkerHeight)
71 {
72  FILE* fp = fopen(nFileName, "r");
73 
74  if(!fp)
75  return NULL;
76 
77  const int strMax = 256, numPix = nMarkerWidth*nMarkerHeight;
78  unsigned char* pixels24 = new unsigned char[numPix*3];
79  char str[strMax+1];
80  int x,y,channel;
81 
82  for(channel=0; channel<3; channel++)
83  for(y=0; y<nMarkerHeight; y++)
84  {
85  for(x=0; x<nMarkerWidth; x++)
86  {
87  str[0] = skipSpaces(fp);
88  readNumber(fp, str+1, strMax);
89  int value = atoi(str);
90  int offset = 3*(x + y*nMarkerWidth) + channel;
91  assert(offset < numPix*3);
92  pixels24[offset] = value;
93  }
94  }
95 
96 
97  fclose(fp);
98 
99  return pixels24;
100 }
101 
102 
103 int main(int argc, char** argv)
104 {
105  unsigned char *patternImage;
106  int width = 16, height = 16;
107 
108  if(argc<3)
109  {
110  printf("ERROR: to few parameters\n");
111  printf("Usage: %s infilename outfilename [width=16] [height=16]\n", argv[0] );
112  printf("Example: %s patt.hiro patt_hiro.ppm 32 32\n", argv[0] );
113  return -1;
114  }
115 
116  const char *inName = argv[1],
117  *outName = argv[2];
118 
119  if( argc >= 4 )
120  width = atoi( argv[3] );
121  if( argc >= 5 )
122  height = atoi( argv[4] );
123 
124  patternImage = readMarkerFile(inName, width,height);
125 
126  if( patternImage )
127  writePPM( outName, patternImage, width, height );
128 
129  return 0;
130 }
char skipSpaces(FILE *fp)
TFSIMD_FORCE_INLINE const tfScalar & y() const
bool writePPM(const char *fileName, unsigned char *data, int width, int height)
unsigned char * readMarkerFile(const char *nFileName, int nMarkerWidth, int nMarkerHeight)
bool readNumber(FILE *fp, char *buffer, int maxLen)
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define NULL
Definition: PocketKnife.h:38
int main(int argc, char **argv)


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