#include <stdio.h>
#include <stdlib.h>
/*11/16/06, D.J. Dinnebeil: extracts a 1D arrray from a 2D image
array based on user input. 2D array is static, 1D array is dynamic. */

void main()
{

unsigned char *image1;
unsigned char image[512][512]; /* Each pixel is a byte, best represented using
                                  unsigned char data type */
FILE *fr; /* File pointer to open the image */
FILE *fw; /* File pointer to write the image */

int i,j,j1,i2,j2;
int rows,cols,highest_intensity; /* The pgm format header includes cols,rows,highest
                                    intensity of the image */

system("djpeg -outfile lenabrite.pgm lenabrite.jpg"); /* djpeg is a unix command, which
                                       converts jpeg to other image formats - see man djpeg */

fr=fopen("lenabrite.pgm","r");  /* Open the pgm file - this is the image written using above command */

fscanf(fr,"P5\n%d %d\n%d\n",&rows,&cols,&highest_intensity); /* P5 represents binary format of 
                                     a  pgm file */

fread(image,1,512*512,fr); /* After the header, pgm has raw image */

fclose(fr);

/* now, lets modify the pixels.. */

for (i=256;i<rows-200;i++)
for (j=256;j<cols-200;j++)
image[i][j]=0;

/* We will write the pgm file first */
fw=fopen("lenabrite_modified.pgm","w");

/* With the header first and then the image in binary */
fprintf(fw,"P5\n512 512\n255\n");
fwrite(image,1,512*512,fw);
fclose(fw);

/*The 'cjpeg' command converts the pgm file into jpeg, which can be opened by any browser*/
 
system("cjpeg -outfile lenabrite_modified.jpg lenabrite_modified.pgm");

printf("Give the range of 1d\n");
scanf("%d%d%d", &i2, &j1, &j2);

image1 = (unsigned char *) calloc (j2-j1+1, sizeof(unsigned char *)) ;

i=0;
 for(j=j1;j<=j2;j++) {

image1[i] = image[i2][j];
printf("image1 data %d\n", image1[i]);
i++;

 }

}
