Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://stackoverflow.com/questions/2693631/read-ppm-file-and-store-it-in-an-array-coded-with-c?fbclid=IwAR3p0n1sx_IIooYmhb2aVdFww0x-FR3Zso_Uw4IHaayPpm5lWQkGTg6SYck
- #include "ppm.h"
- #include <stdio.h>
- #include <stdlib.h>
- PPMImage *readPPM(const char *filename)
- {
- 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] != '6' && buff[1] != '3')) {
- printf("SDADASD %c%c\n", buff[0], buff[1]);
- fprintf(stderr, "Invalid image format (must be 'P6' or P3)\n");
- exit(1);
- }
- //alloc memory form image
- img = (PPMImage *)malloc(sizeof(PPMImage));
- if (!img) {
- fprintf(stderr, "Unable to allocate memory\n");
- exit(1);
- }
- if (buff[1] == '6') {
- img->type = 0;
- } else {
- img->type = 1;
- }
- //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);
- }
- //read rgb component
- if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
- fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
- exit(1);
- }
- if (img->type == 1) {
- img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));
- int i;
- for (i = 0; i < img->x * img->y; ++i) {
- int r, g, b;
- fscanf(fp, "%d%d%d", &r, &g, &b);
- img->data[i].red = (unsigned char)r;
- img->data[i].green = (unsigned char)g;
- img->data[i].blue = (unsigned char)b;
- }
- fclose(fp);
- return img;
- }
- //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
- int i;
- int j;
- for (i = 0; i < img->y; i++) {
- for (j = 0; j < img->x; j++) {
- unsigned char r, g, b;
- r = fgetc(fp);
- g = fgetc(fp);
- b = fgetc(fp);
- img->data[i * img->x + j].red = r;
- img->data[i * img->x + j].green = g;
- img->data[i * img->x + j].blue = b;
- }
- }
- fclose(fp);
- return img;
- }
- void writePPM(const char *filename, PPMImage *img)
- {
- FILE *fp;
- //open file for output
- fp = fopen(filename, "wb");
- if (!fp) {
- fprintf(stderr, "Unable to open file '%s'\n", filename);
- exit(1);
- }
- if (img->type == 1) {
- fprintf(fp, "P3\n");
- fprintf(fp, "%d %d\n", img->x, img->y);
- fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);
- int i;
- for (i = 0; i < img->x * img->y; ++i) {
- int r = (int)img->data[i].red;
- int g = (int)img->data[i].green;
- int b = (int)img->data[i].blue;
- fprintf(fp, "%d %d %d\n", r, g, b);
- }
- fclose(fp);
- }
- //write the header file
- //image format
- fprintf(fp, "P6\n");
- //comments
- fprintf(fp, "# Created by %s\n",CREATOR);
- //image size
- fprintf(fp, "%d %d\n",img->x,img->y);
- // rgb component depth
- fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);
- // pixel data
- fwrite(img->data, 3 * img->x, img->y, fp);
- fclose(fp);
- }
- void changeColorPPM(PPMImage *img)
- {
- int i;
- if(img){
- for(i=0;i<img->x*img->y;i++){
- img->data[i].red=RGB_COMPONENT_COLOR - img->data[i].red;
- img->data[i].green=RGB_COMPONENT_COLOR - img->data[i].green;
- img->data[i].blue=RGB_COMPONENT_COLOR - img->data[i].blue;
- }
- }
- }
- void writePGM(const char *filename, PPMImage *img) {
- FILE *fp;
- //open file for output
- fp = fopen(filename, "wb");
- if (!fp) {
- fprintf(stderr, "Unable to open file '%s'\n", filename);
- exit(1);
- }
- fprintf(fp, "P2\n");
- fprintf(fp, "%d %d\n", img->x, img->y);
- fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);
- int i;
- int j;
- for (i = 0; i < img->y; ++i) {
- for (j = 0; j < img->x; ++j) {
- fprintf(fp, "%d", img->data[i * img->x + j].red);
- if (j != img->x) {
- fprintf(fp, " ");
- }
- }
- fprintf(fp, "\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement