Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //Colour structure, holds a triple of ints for RGB
- typedef struct colour {
- int r,g,b;
- } colour;
- //PPM structure - holds the format of the file (P3 or P6)
- // - holds the width, height and maximum value of pixels
- // - and the 2D array of colour values
- typedef struct ppm {
- char * format;
- int width, height, max;
- colour * pixels[];
- } ppm;
- colour * new_colour(int r, int g, int b) {
- colour * c = (colour *)malloc(sizeof(colour));
- c->r = r;
- c->g = g;
- c->b = b;
- return c;
- }
- //PPM METHODS
- ppm * get_ppm(FILE * ppm_file){
- ppm * my_ppm = malloc(sizeof(ppm));
- char format[20];
- char comment[100];
- int n, m, max;
- fgets(format, 10, ppm_file);
- fgets(comment, 100, ppm_file);
- char dimension_line[100];
- fgets(dimension_line, 100, ppm_file);
- sscanf(dimension_line,"%d %d", &n, &m);
- char max_line[20];
- fgets(max_line, 20, ppm_file);
- sscanf(max_line, "%d", &max);
- int i;
- for (i = 0; i<=(m*n - 1); i++) {
- my_ppm->pixels[i] = malloc(sizeof(colour));
- int r, g, b;
- fscanf(ppm_file,"%d %d %d ",&r,&g,&b);
- printf("p[%d] %d %d %d\n", i,r,g,b);
- my_ppm->pixels[i] = new_colour(r,g,b);
- printf("SAVE: p[%d] %d %d %d\n", i,my_ppm->pixels[i]->r,my_ppm->pixels[i]->g,my_ppm->pixels[i]->b);
- }
- my_ppm->format = format;
- my_ppm->width = n;
- my_ppm->height = m;
- my_ppm->max = max;
- return my_ppm;
- }
- void show_ppm(ppm * image) {
- printf("Format: %s\nW:%d H:%d\nMAX:%d\n\n", image->format,
- image->width, image-> height,
- image->max);
- int i;
- for (i = 0; i<=(image->height)*(image->width) - 1; i++) {
- int r = image->pixels[i]->r;
- int g = image->pixels[i]->g;
- int b = image->pixels[i]->b;
- printf("p[%d]: (%d,%d,%d)\n",i,r,g,b);
- }
- }
- ppm * encode(ppm * img, char * text) {
- int arr_length = (img->width)*(img->height);
- size_t strlen(const char *str);
- int str_length = strlen(text);
- int i=-1;
- srand(time(NULL));
- while (str_length >= 0) {
- int r = 1 + rand() % (int)(arr_length/str_length);
- i += r;
- img->pixels[i]->r = (int)(text[i]);
- arr_length -= r;
- str_length--;
- }
- return img;
- }
- int main() {
- ppm * my_image = get_ppm(fopen("sin.ppm", "r"));
- printf("%d", my_image->pixels[0]->r);
- //show_ppm(my_image);
- }
Advertisement
Add Comment
Please, Sign In to add comment