File: ppm2array.cpp

package info (click to toggle)
glui 2.1.0.beta-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,068 kB
  • ctags: 1,302
  • sloc: cpp: 9,043; ansic: 857; makefile: 235
file content (56 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download
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
#include "imagetype.h"
#include <stdio.h>

void main( int argc, char *argv[] )
{
  int        i;
  ImagePPM   img;
  FILE      *output;
  char       basename[200];

  if ( argc != 3 ) {
    fprintf( stderr, "USAGE: %s input.ppm output.cpp\n", argv[0] );
    return;
  }

  img.read( argv[1] );

  if ( img.valid ) {
    strcpy( basename, argv[1] );
    basename[ strlen(basename)-4 ] = '\0';
    
    output = fopen( argv[2], "w" );
    if ( NOT output ) {
      fprintf( stderr, "ERROR: File '%s' could not be opened for writing\n",
	       argv[2] );
      return;
    }

    img.flip_v(); /* Opengl bitmaps are specified bottom-to-top */

    fprintf( output, "\n\n");
    fprintf( output, "int %s[] = {", basename );
    fprintf( output, "    %d, %d,   /* width, height */\n", 
	     img.x_size, img.y_size );

    fprintf( output, "    " );  
    for( i=0; i<img.x_size * img.y_size; i++ ) {
      fprintf( output, "%3d,%3d,%3d,  ", 
	       img.pixels[i*3+0],
	       img.pixels[i*3+1],
	       img.pixels[i*3+2] );
      if ( (i%5) == 4 ) {
	fprintf( output, "\n    " );
      }
    }

    fprintf( output, "\n};\n" );
    fclose( output );
  }
  else {
    fprintf( stderr, "ERROR: Image '%s' invalid\n", argv[1] );
  }
  
}