/* SubLCD, the program.
   It reads a picture in the raw ppm/pnm P6 format,
   and outputs the same picture in half size, but which looks
   just as detailed horizontally as the original,
   if viewed on a typical LCD screen.
Typical use:
  ./SubLCD < input_file_in_binary_raw.ppm > halved_output_file.ppm
Typical compiler command:
  gcc -o SubLCD SubLCD.c
*/


/* MIT License format:

Copyright (c) 2006, Kim Øyhus, kim@oyhus.no
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


#include <stdio.h>
#include <string.h>

     int
main()
{
    signed int x,y;
    int Width, Heigth;
    char s[1002];
    int lines[2048][3];
    

/* For a PNM P6 file to be readable here, it should have the format:
P6            F.ex:   P6
width height          320 240
colours               256
raw data              Yujbdfn2#A...
*/
    fgets(s, 100, stdin);
    if(strncmp("P6",s,2) !=0)
	fprintf(stderr, "This is not a PNM, P6 file\n");
    do  // Removal of comments
	fgets(s, 1000, stdin);
    while(s[0]=='#');
    sscanf(s, "%d %d", &Width, &Heigth);
    fprintf(stderr, "Width=%d Heigth=%d\n", Width, Heigth);
    fgets(s, 100, stdin);  // Removal of last line
    
    printf("P6\n%d %d\n255\n", Width/2, Heigth/2);
    
    for(y=0; y<Heigth-1; y+=2)
    {
	for(x=0; x<Width; x++)
	{
	    lines[x][0] = fgetc(stdin);
	    lines[x][1] = fgetc(stdin);
	    lines[x][2] = fgetc(stdin);
	}
	for(x=0; x<Width; x++)
	{
	    lines[x][0] += fgetc(stdin);
	    lines[x][1] += fgetc(stdin);
	    lines[x][2] += fgetc(stdin);
	}
	for(x=0; x<Width-1; x+=2)
	{
	    if(x%2==0)
	    {
		putchar(lines[x  ][0]>>1);
		putchar(lines[x+1][1]>>1);
		putchar(lines[x+2][2]>>1);
	    }	    
	}
    }
}

