Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ARToolKitPlus/tools/PATT_to_PPM/src/main.cpp
blob: efe49bf427a165eb5dd36ab9c1103150cfad6e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

//
//
//  This tool will read ARToolKit pattern files (with variable size) and save
//  them as Portable Pixmap (.ppm) files.
//
//  Usage: PATT_to_PPM [infilename] [outfilename] [width] [height]
//
//
//  Thanks to Antonio Bleile for contributing the PPM writer code
// 


#include <stdio.h>
#include <assert.h>
#include <stdlib.h>



char
skipSpaces(FILE* fp)
{
	char ch = ' ';

	while( ch==' ' && !feof(fp))
		ch = fgetc(fp);

	return ch;
}


bool
readNumber(FILE* fp, char* buffer, int maxLen)
{
	char ch = 0;
	int i=0;

	do {
		ch = fgetc(fp);
		if(ch>='0' && ch<='9')
			buffer[i++] = ch;
	} while(!feof(fp) && i<maxLen-1 && ch!=' ');


	buffer[i++] = 0;
	return i<maxLen && !feof(fp);
}


bool
writePPM( const char *fileName, unsigned char *data, int width, int height )
{
	FILE *file = fopen( fileName, "wb" );
	if( !file ){
		printf( "Could not open file %s for writing\n", fileName );
		return false;
	}

	// error checking omitted
	fprintf( file, "P6\n# generated by patt_to_ppm\n%d %d\n255\n", width, height );
	fwrite( data, width*height*3, 1, file );

	fclose( file );

	return true;
}


unsigned char *
readMarkerFile(const char* nFileName, int nMarkerWidth, int nMarkerHeight)
{
	FILE* fp = fopen(nFileName, "r");

	if(!fp)
		return NULL;

	const int strMax = 256, numPix = nMarkerWidth*nMarkerHeight;
	unsigned char* pixels24 = new unsigned char[numPix*3];
	char str[strMax+1];
	int x,y,channel;

	for(channel=0; channel<3; channel++)
		for(y=0; y<nMarkerHeight; y++)
		{
			for(x=0; x<nMarkerWidth; x++)
			{
				str[0] = skipSpaces(fp);
				readNumber(fp, str+1, strMax);
				int value = atoi(str);
				int offset = 3*(x + y*nMarkerWidth) + channel;
				assert(offset < numPix*3);
				pixels24[offset] = value;
			}
		}


	fclose(fp);

	return pixels24;
}


int main(int argc, char** argv)
{
	unsigned char *patternImage;
	int width = 16, height = 16;

	if(argc<3)
	{
		printf("ERROR: to few parameters\n");
		printf("Usage: %s infilename outfilename [width=16] [height=16]\n", argv[0] );
		printf("Example: %s patt.hiro patt_hiro.ppm 32 32\n", argv[0] );
		return -1;
	}

	const char	*inName = argv[1],
				*outName = argv[2];

	if( argc >= 4 )
		width = atoi( argv[3] );
	if( argc >= 5 )
		height = atoi( argv[4] );

	patternImage = readMarkerFile(inName, width,height);

	if( patternImage )
		writePPM( outName, patternImage, width, height );

	return 0;
}