mardi 24 janvier 2017

Programming C Struct manipulation with If statement

I am trying to create a basic steganography application where the user can encode and decode PPM images. To encode an image with a secret message, first i take the image and convert it into its raw value i.e x,y and rgb. Then i take the message convert it to an ascii integers to then be placed into the red values at a random int position on the x y axis. I am stuck on how to edit the struct to change the red value to the ascii value where the position x,y is at a certain value.Here is what i have so far any help would be appreciated as i have just started to learn c.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>

typedef struct {
 unsigned char red,green,blue;
} PPMPixel;

typedef struct {
 int x, y;
 PPMPixel *data;
} PPMImage;

#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{



//change code to look like my own and different 
     char buff[16];
     PPMImage *img;
     FILE *fp;
     int c, rgb_comp_color;
     //open PPM file for reading
     fp = fopen(filename, "rb");
     if (!fp) {
          fprintf(stderr, "Unable to open file '%s'\n", filename);
          exit(1);
     }

     //read image format
     if (!fgets(buff, sizeof(buff), fp)) {
          perror(filename);
          exit(1);
     }

//check the image format
if (buff[0] != 'P' || buff[1] != '3') {
     fprintf(stderr, "Invalid image format (must be 'P3')\n");
     exit(1);
}else{
    printf("P3\n");
}

//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}

size_t linecount = 0;

//check for comments
c = getc(fp);

   while (c == '#') {

  while (getc(fp) != '\n') ;
     c = getc(fp);

}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
     fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
     exit(1);
}else{
    printf("Height: %d\n",img->x);
    printf("Width: %d\n",img->y);

}

//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
     fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
     exit(1);
}else{
    printf("%d\n",rgb_comp_color );
}

//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
     fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
     exit(1);
}

while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));

if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}

//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
     fprintf(stderr, "Error loading image '%s'\n", filename);
     exit(1);
}

fclose(fp);
return img;
}

void outputRGB(PPMImage *img)
{

int i;
if(img){

     for(i=0;i<img->x*img->y;i++){
         printf("R: %d ",img->data[i].red );
         printf("G: %d ",img->data[i].green );
         printf("B: %d\n ",img->data[i].blue );

     }
}
}

struct PPM * encode(char * text, PPMImage * img)
{
    //creating new instance of the image to compare /debug
    PPMImage *imgcopy;
    imgcopy = img;

    //printf("Test: %s",text);


    //convert secret message to ascii code
    int i,ascii;
    int total = 0;
    time_t t;
    srand((unsigned) time(&t));

    for(i = 0; text[i]; i++){

        //printf("Char: %c\n",text[i]);
        ascii = text[i];
        //printf("Ascii: %d\n",ascii);
        //create random number between 0 and random max

        total = rand() % 100;
        //printf("Random Number: %d\n",total);

        //at position random we set the red bit equal to ascii number           
        //stuck here unable to pin point to exact struct i want to edit 

            for(i=0;i<img->x*img->y;i++){
            img->data[0].red = total;
            printf("R: %d\n",img->data[0].red);
            }




    }







    //printf("Ascii Value for A %d\n",x);

    //int i;
    //for(i = 0; i < length; i = i + 1){
    //  printf("R: %d\n",imgcopy->data[i].red );
    //  if(binary[i]== '1'){
    //      img->data[i].red=img->data[i].red + 1;
    //  }
    //  printf("New R: %d\n ",imgcopy->data[i].red );
    //}

    //then return the new image 



}

//char * decode(struct PPMImage * i1, struct PPMImage * i2){

//compare difference in number of bits in red pixels
//then construct a binary number from it.
//then translate and decode.




//}



int main(){
PPMImage *image;
image = readPPM("aab.ppm");
encode("The Secret Message",image);
 // outputRGB(image);

  //need to write input statements 




}

Aucun commentaire:

Enregistrer un commentaire