Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
raw_to_png.cpp
Go to the documentation of this file.
1 #include <png.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 unsigned int height = 256;
6 unsigned int width = 256;
7 int rowoffset = 0;
8 int coloffset = 0;
9 
10 unsigned short * Rread( const char *myfile )
11 {
12  unsigned short *tm = (unsigned short*) malloc( 512*512*sizeof (unsigned short) );
13  int i;
14  FILE *fp = VSFileSystem::vs_open( myfile, "rb" );
15  if (!fp)
16  return tm;
17  VSFileSystem::Read( tm, sizeof (unsigned short), 512*512, fp );
19  unsigned short *smaller = (unsigned short*) malloc( 256*256*sizeof (unsigned short) );
20  for (i = 0; i < 256; i++)
21  memcpy( smaller+(i*256), tm+rowoffset+( (i+coloffset)*512 ), sizeof (unsigned short)*256 );
22  if (rowoffset)
23  for (i = 0; i < 256; i++)
24  smaller[i*256] = tm[rowoffset+ -1+( (i+coloffset)*512 )];
25  for (i = 0; i < 64*256; i++) {
26  int count = 0;
27  int j;
28  smaller[i] = 256;
29  }
30  for (i = 0; i < 256; i++)
31  for (int j = 0; j < 64; j++)
32  smaller[i*256+j] = 256;
33  /*
34  * while (count<256) {
35  * for (j=0;j<16;j++,count++) {
36  * if (smaller[i*256+count]!=0)
37  * j=0;
38  * if (count==256)
39  * break;
40  * }
41  * if (j>5&&count>0) {
42  * for (int k=count-1;k>=0;k--) {
43  * if (smaller[i*256+count]==0) {
44  * smaller[i*256+count]=05;
45  * }else {
46  * break;
47  * }
48  * }
49  * }
50  * }
51  */
52  if (coloffset)
53  memcpy( smaller, tm+rowoffset+(coloffset-1)*512, sizeof (unsigned short)*256 );
54  free( tm );
55  return smaller;
56 }
57 
58 void Wwrite( const char *myfile, unsigned short *data )
59 {
60  FILE *fp = VSFileSystem::vs_open( myfile, "wb" );
61  png_structp png_ptr = png_create_write_struct
62  ( PNG_LIBPNG_VER_STRING, (png_voidp) NULL, NULL, NULL );
63  //user_error_fn, user_warning_fn);
64  if (!png_ptr)
65  return;
66  png_infop info_ptr = png_create_info_struct( png_ptr );
67  if (!info_ptr) {
68  png_destroy_write_struct( &png_ptr,
69  (png_infopp) NULL );
70  return;
71  }
72  if ( setjmp( png_ptr->jmpbuf ) ) {
73  png_destroy_write_struct( &png_ptr, &info_ptr );
75  return;
76  }
77  png_init_io( png_ptr, fp );
78  png_set_filter( png_ptr, 0,
79  PNG_FILTER_NONE );
80  png_set_compression_level( png_ptr, Z_BEST_COMPRESSION );
81 
82  /* set other zlib parameters */
83  png_set_compression_mem_level( png_ptr, 8 );
84  png_set_compression_strategy( png_ptr,
85  Z_DEFAULT_STRATEGY );
86  png_set_compression_window_bits( png_ptr, 15 );
87  png_set_compression_method( png_ptr, 8 );
88 
89  png_set_IHDR( png_ptr, info_ptr, width, height,
90  16, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
91  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
92 
93  png_write_info( png_ptr, info_ptr );
94  png_set_swap( png_ptr );
95  png_byte **row_pointers = new png_byte*[height];
96  for (unsigned int i = 0; i < height; i++)
97  row_pointers[i] = (png_byte*) &data[i*width];
98  png_write_image( png_ptr, row_pointers );
99  png_write_end( png_ptr, info_ptr );
100  png_destroy_write_struct( &png_ptr, &info_ptr );
101  //png_write_flush(png_ptr);
103  free( data );
104  delete[] row_pointers;
105 }
106 
107 int main( int argc, char **argv )
108 {
109  if (argc > 3)
110  sscanf( argv[3], "%d", &rowoffset );
111  if (argc > 4)
112  sscanf( argv[4], "%d", &coloffset );
113  Wwrite( argv[2], Rread( argv[1] ) );
114  return 0;
115 }
116