| #include <stdio.h>
#include <stdlib.h>
unsigned char stripe [1000][7];
static int prologue[21] = {
165, 0, 6, 64, 5, 0, 0, 0, 0,
27, 42, 128,
27, 42, 7, 115, 48,
27, 42, 7, 99 };
static int esc_3[5] = { 27, 42, 3, 0, 56 };
// 1: <escape>
// 2: "*"
// 3: <3> (drop code)
// 4: 256*pixels to drop
// 5: pixels to drop
static int esc_4[30] = { 27, 42, 4, 0, 250,
0, 1, 1, 1, 7, 49,
0, 32, 4, 1, 2, 250,
0, 0, 0, 0, 0, 50, 51, 52, 53 };
// 1: <escape>
// 2: "*"
// 3: <4> (raster code)
// 4: 256*bytes in raster
// 5: bytes in raster
// 12:
// 13:
// 14: 256*start column
// 15: start column
// 16:
// 17:
static int raster_ptr = 0;
static int raster_buff[5200][7];
static int raster_buff2[5200][7*8];
int raster_header( FILE *fp, int start_column, int drop_pixels,
int data_pixels )
{
int i;
esc_3[3] = drop_pixels / 256;
esc_3[4] = drop_pixels - ( esc_3[3] * 256 );
esc_4[3] = (data_pixels+26) / 256;
esc_4[4] = (data_pixels+26) - ( esc_4[3] * 256 );
esc_4[11] = (data_pixels/7) / 256;
esc_4[12] = (data_pixels/7) - ( esc_4[3] * 256 );
esc_4[13] = start_column / 256;
esc_4[14] = start_column - ( esc_4[13] * 256 );
for ( i=0; i<5; i++ )
if ( fp == NULL ) putchar( esc_3[i] );
else fputc( esc_3[i], fp );
for ( i=0; i<26; i++ )
if ( fp == NULL ) putchar( esc_4[i] );
else fputc( esc_4[i], fp );
return ( start_column );
}
int get_line( FILE *fpin, char *line_str )
{
char buff[256];
strcpy( buff, "#" );
while ( strncmp( buff, "#", 1 ) == 0 )
if ( fpin == NULL )
gets( buff );
else
fgets( buff, sizeof(buff), fpin );
strcpy( line_str, buff );
return( strlen( buff ) );
}
int get_raster( FILE *fpin, int inx, int raster[], int ppm_type )
{
int i = 1, j, cnt = 0;
int WIDE = 1;
if (( ppm_type == 2 ) || ( ppm_type == 3 ))
WIDE = 2;
if ( ppm_type < 2 )
{
while (( cnt < inx*WIDE ) && ( i != EOF ))
{
if ( fpin == NULL )
i = getchar();
else
i = fgetc( fpin );
if ( i == 48 )
raster[cnt++] = 0;
else if ( i == 49 )
raster[cnt++] = 1;
}
} else if (( ppm_type == 2 ) || ( ppm_type == 3 ))
{
while (( cnt < inx*WIDE ) && ( i != EOF ))
{
if ( fpin == NULL )
i = scanf( "%d", &j );
else
i = fscanf( fpin, "%d", &j );
if ( j > 85 )
raster[cnt++] = 0;
else
raster[cnt++] = 1;
if ( j > 170 )
raster[cnt++] = 0;
else
raster[cnt++] = 1;
}
} else if ( ppm_type == 6 )
{
while (( cnt < inx*WIDE ) && ( i != EOF ))
{
if ( fpin == NULL )
{
i = getchar();
i = getchar();
i = getchar();
} else {
i = fgetc( fpin );
i = fgetc( fpin );
i = fgetc( fpin );
}
if ( i > 127 )
raster[cnt++] = 0;
else
raster[cnt++] = 1;
}
} else {
puts("?strange ppm file");
exit( 1 );
}
return( cnt >= inx*WIDE );
}
// #define WIDE 1
int put_raster( int inx, int raster[], FILE *fp, int ppm_type )
{
int col, i, j;
int padx;
int raslen;
int WIDE = 1;
if (( ppm_type == 2 ) || ( ppm_type == 3 ))
WIDE = 2;
for (i=0; i<inx*WIDE; i++)
raster_buff2[i][raster_ptr] = raster[i];
if ( ++raster_ptr == 7*8 )
{
for ( j=0; j<inx*WIDE; j++ )
for ( i=6; i>=0; i-- )
{
raster_buff[j][i] =
raster_buff2[j][i*8] +
2 * raster_buff2[j][i*8+1] +
4 * raster_buff2[j][i*8+2] +
8 * raster_buff2[j][i*8+3] +
16 * raster_buff2[j][i*8+4] +
32 * raster_buff2[j][i*8+5] +
64 * raster_buff2[j][i*8+6] +
128 * raster_buff2[j][i*8+7];
}
/*
for ( j=0; j<inx; j++ )
for ( i=6; i>=0; i-- )
raster_buff[j][i+7] = raster_buff[j][i];
for ( j=0; j<inx; j++ )
for ( i=6; i>=0; i-- )
raster_buff[j][i] = raster_buff[j][13-i];
*/
raslen = 0;
for ( i=0; i<inx*WIDE; i++ )
if (( raster_buff[i][0] + raster_buff[i][1] + raster_buff[i][2] +
raster_buff[i][3] + raster_buff[i][4] + raster_buff[i][5] +
raster_buff[i][6] ) != 0 )
raslen = i;
// padx = ( 7*raslen*WIDE ) / 112;
padx = ( 7*raslen ) / 112;
padx = ( padx+1 ) * 112;
raster_header( fp, 100, 56, padx );
for ( col=0; col<(padx/7); col++ )
{
// for ( j=0; j<WIDE; j++ )
for ( i=6; i>=0; i-- )
if ( fp == NULL )
putchar( raster_buff[col][i] );
else
fputc( raster_buff[col][i], fp );
}
raster_ptr = 0;
for (i=0; i<inx*WIDE; i++)
for (j=0; j<56; j++)
raster_buff[i][j] = 0;
}
}
int main( int argc, char *argv[] )
{
int i, j, k, p;
int col, dat;
int cnt;
int inx, iny;
int ppm_type = 0;
int okdebug = 0;
int raster[1024*1024];
char infile[256];
char outfile[256];
char buff[256];
unsigned char c;
FILE *fp, *fpin, *fdbg;
if ( okdebug )
{
fdbg = fopen( "/tmp/oki2010dbg", "w" );
if ( fdbg == NULL )
okdebug = 0;
else
for ( i=0; i<argc; i++ )
fprintf( fdbg, "%d: %s\n", i, argv[i] );
}
if (argc > 1 )
strcpy( infile, argv[1] );
else strcpy( infile, "/dev/stdin" );
if (argc > 2 )
strcpy( outfile, argv[2] );
else strcpy( outfile, "/dev/stdout" );
if ( argc < 2 )
{
fpin = NULL;
} else {
fpin = fopen( infile, "r" );
if ( fpin == NULL )
{
printf( "oki2010: cannot open %s\n", infile );
printf( "oki2010: error %d\n", errno );
perror( "oki2010" );
if ( okdebug )
{
fprintf( fdbg, "error %d on input\n", errno );
fclose( fdbg );
}
exit (1);
}
}
/*
if ( fpin != NULL )
{
fgets( buff, sizeof(buff), fpin );
if ( strncmp( "P1", buff, 2 ) == 0 )
ppm_type = 1;
else if ( strncmp( "P2", buff, 2 ) == 0 )
ppm_type = 2;
else if ( strncmp( "P3", buff, 2 ) == 0 )
ppm_type = 3;
fgets( buff, sizeof(buff), fpin );
fgets( buff, sizeof(buff), fpin );
} else {
gets( buff );
if ( strncmp( "P1", buff, 2 ) == 0 )
ppm_type = 1;
else if ( strncmp( "P2", buff, 2 ) == 0 )
ppm_type = 2;
else if ( strncmp( "P3", buff, 2 ) == 0 )
ppm_type = 3;
gets( buff );
gets( buff );
}
*/
get_line( fpin, buff );
if ( strncmp( "P1", buff, 2 ) == 0 )
ppm_type = 1;
else if ( strncmp( "P2", buff, 2 ) == 0 )
ppm_type = 2;
else if ( strncmp( "P3", buff, 2 ) == 0 )
ppm_type = 3;
else if ( strncmp( "P6", buff, 2 ) == 0 )
ppm_type = 6;
get_line( fpin, buff );
sscanf( buff, "%d %d", &inx, &iny );
// printf( "oki2010: %s is %dx%d\n", infile, inx, iny );
if ( ppm_type > 1 )
get_line( fpin, buff );
if ( argc < 3 )
{
fp = NULL;
} else {
fp = fopen( outfile, "w" );
if ( fp == NULL )
{
printf( "oki2010: cannot open %s\n", outfile );
printf( "oki2010: error %d\n", errno );
perror( "oki2010" );
if ( okdebug )
{
fprintf( fdbg, "error %d on output\n", errno );
fclose( fdbg );
}
exit (1);
}
}
for (i=0; i<21; i++ )
if ( fp == NULL )
putchar( prologue[i] );
else
fputc( prologue[i], fp );
cnt = 0;
j = get_raster( fpin, inx, raster, ppm_type );
while ( j )
{
put_raster( inx, raster, fp, ppm_type );
if (( ppm_type == 2 ) || ( ppm_type == 3 ))
put_raster( inx, raster, fp, ppm_type );
j = get_raster( fpin, inx, raster, ppm_type );
cnt++;
// printf( "%d\n", cnt );
}
printf("lines=%d\n", cnt);
if ( fp == NULL )
printf( "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
27, 42, 7, 101, 165, 0, 6, 64, 5, 1, 0, 0, 1, 0, 0, 0 );
else
fprintf( fp, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
27, 42, 7, 101, 165, 0, 6, 64, 5, 1, 0, 0, 1, 0, 0, 0 );
fclose(fp);
fclose(fpin);
if ( okdebug )
fclose(fdbg);
}
|