Guest User

Untitled

a guest
Feb 22nd, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Colour structure, holds a triple of ints for RGB
  4. typedef struct colour {
  5.     int r,g,b;
  6. } colour;
  7.  
  8. //PPM structure - holds the format of the file (P3 or P6)
  9. //              - holds the width, height and maximum value of pixels
  10. //              - and the 2D array of colour values
  11. typedef struct ppm {
  12.     char * format;
  13.     int width, height, max;
  14.     colour * pixels[];
  15. } ppm;
  16.  
  17. colour * new_colour(int r, int g, int b) {
  18.     colour * c = (colour *)malloc(sizeof(colour));
  19.     c->r = r;
  20.     c->g = g;
  21.     c->b = b;
  22.     return c;
  23. }
  24.  
  25. //PPM METHODS
  26. ppm * get_ppm(FILE * ppm_file){
  27.     ppm * my_ppm = malloc(sizeof(ppm));
  28.     char format[20];
  29.     char comment[100];
  30.     int n, m, max;
  31.  
  32.     fgets(format, 10, ppm_file);
  33.     fgets(comment, 100, ppm_file);
  34.     char dimension_line[100];
  35.     fgets(dimension_line, 100, ppm_file);
  36.     sscanf(dimension_line,"%d %d", &n, &m);
  37.     char max_line[20];
  38.     fgets(max_line, 20, ppm_file);
  39.     sscanf(max_line, "%d", &max);
  40.    
  41.     int i; 
  42.     for (i = 0; i<=(m*n - 1); i++) {
  43.         my_ppm->pixels[i] = malloc(sizeof(colour));
  44.         int r, g, b;
  45.         fscanf(ppm_file,"%d %d %d ",&r,&g,&b);
  46.         printf("p[%d] %d %d %d\n", i,r,g,b);
  47.         my_ppm->pixels[i] = new_colour(r,g,b);
  48.         printf("SAVE: p[%d] %d %d %d\n", i,my_ppm->pixels[i]->r,my_ppm->pixels[i]->g,my_ppm->pixels[i]->b);
  49.     }
  50.  
  51.     my_ppm->format = format;
  52.     my_ppm->width = n;
  53.     my_ppm->height = m;
  54.     my_ppm->max = max;
  55.     return my_ppm;
  56. }
  57.  
  58. void show_ppm(ppm * image) {
  59.     printf("Format: %s\nW:%d H:%d\nMAX:%d\n\n", image->format,
  60.                          image->width, image-> height,
  61.                          image->max);
  62.     int i;
  63.     for (i = 0; i<=(image->height)*(image->width) - 1; i++) {
  64.         int r = image->pixels[i]->r;
  65.         int g = image->pixels[i]->g;
  66.         int b = image->pixels[i]->b;
  67.         printf("p[%d]: (%d,%d,%d)\n",i,r,g,b);
  68.     }
  69. }
  70.  
  71. ppm * encode(ppm * img, char * text) {
  72.     int arr_length = (img->width)*(img->height);
  73.     size_t strlen(const char *str);
  74.     int str_length = strlen(text);
  75.  
  76.     int i=-1;
  77.     srand(time(NULL));
  78.     while (str_length >= 0) {
  79.         int r = 1 + rand() % (int)(arr_length/str_length);
  80.         i += r;
  81.         img->pixels[i]->r = (int)(text[i]);
  82.         arr_length -= r;
  83.         str_length--;
  84.     }
  85.     return img;
  86. }
  87.  
  88. int main() {
  89.     ppm * my_image = get_ppm(fopen("sin.ppm", "r"));
  90.     printf("%d", my_image->pixels[0]->r);
  91.     //show_ppm(my_image);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment