Advertisement
Guest User

Untitled

a guest
Nov 20th, 2010
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. //##################################### File ppmtools.c #################################
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <sys/wait.h>
  8. #include <math.h>
  9.  
  10. #include "ppmtools.h"
  11.  
  12. #define READ_BUFFER_LEN 16
  13.  
  14. char* readType(FILE *fp) {
  15.     char buf[READ_BUFFER_LEN];
  16.     char *t;
  17.  
  18.     //get line of the file into read buffer -> type of ppm
  19.     t = fgets(buf, READ_BUFFER_LEN, fp);
  20.     //check if it reads something and if it is a P6 type
  21.     if (t == NULL) {
  22.         printf("could not read type from file\n");
  23.         return "";
  24.     }
  25.  
  26.     if (strncmp(buf, "P6\n", 3) != 0) {
  27.         printf("type is not P6\n");
  28.         return "";
  29.     } else {
  30.         printf("set type as P6\n");
  31.         //set type as P6
  32.         return "P6";
  33.     }
  34. }
  35.  
  36. unsigned int readNextHeaderValue(FILE *fp) {
  37.     char buf[READ_BUFFER_LEN];
  38.     char *t;
  39.     int r;
  40.     int value;
  41.  
  42.     printf("read next header value\n");
  43.     //load next line
  44.     t = fgets(buf, READ_BUFFER_LEN, fp);
  45.     if (t == NULL) {
  46.         printf("Error reading from file");
  47.         return 0;
  48.     }
  49.  
  50.     //read value as unsigned int
  51.     r = sscanf(buf, "%u", &value);
  52.  
  53.     //check if the read was successful
  54.     if (r != 1) {
  55.         printf("Could not read width value\n");
  56.         return 0;
  57.     } else {
  58.         return value;
  59.     }
  60. }
  61.  
  62. header* getImageHeader(FILE *fp) {
  63.     //alloc header
  64.     header* h = malloc(sizeof (header));
  65.  
  66.     //check if file pointer is null
  67.     if (fp == NULL) {
  68.         //file pointer is null
  69.         return NULL;
  70.     }
  71.  
  72.     //get type
  73.     char* type = readType(fp);
  74.     if (strcmp(type, "") != 0) {
  75.         //write type
  76.         strcpy(h->type, type);
  77.     } else {
  78.         //error reading type
  79.         return NULL;
  80.     }
  81.  
  82.     h->width = readNextHeaderValue(fp);
  83.     h->height = readNextHeaderValue(fp);
  84.     h->depth = readNextHeaderValue(fp);
  85.  
  86.     //check for successful read and valid color range
  87.     if (h->depth != 255 || h->width == 0 || h->height == 0) {
  88.         //printf("error reading values\n");
  89.         return NULL;
  90.     }
  91.  
  92.     return h;
  93. }
  94. //NOTE:
  95. // The program fails if the first byte of the image is equal to 32 (ascii for space char). because
  96. // the fscanf in eats the space (ascii 32) and the image is read with one byte less
  97.  
  98. int getImageRow(int pixelNo, pixel* row, FILE *fp) {
  99.  
  100.     int i;
  101.     byte b;
  102.     //reading line
  103.  
  104.     for (i = 0; i < pixelNo; i++) {
  105.         //set pixel values
  106.         if (fread(&row[i], sizeof (b), 3 * sizeof (b), fp) < 1)
  107.             return -1;
  108.     }
  109.  
  110.     return pixelNo;
  111. }
  112.  
  113. int sh_putImageRow(pixel* row, image_struct *image, int line) {
  114.     int col;
  115.     for (col = 0; col < image->width; col++) {
  116.         if (!memcpy(&image->pixel_data[line][col],&row[col], 3 * sizeof (byte))) {
  117.             //printf("Error copying from row to pixel_data[%d],line\n");
  118.             return -1;
  119.         }
  120.     }
  121.     return line;
  122. }
  123.  
  124. /*int fillShmWithImage(FILE* fp, image_struct* image) {
  125.     int i, j;
  126.     byte b;
  127.  
  128.     for (i = 0; i < image->height; i++) {//linhas
  129.         for (j = 0; j < image->width; j++) {//colunas <-> largura
  130.             //set pixel values
  131.             if (fread(&image->pixel_data[i][j], sizeof (b), 3 * sizeof (b), fp) < 1)
  132.                 return -1;
  133.         }
  134.     }
  135.     printf("Coijo!\n");
  136.     return image->width* image->height;
  137. }*/
  138.  
  139. void invertRow(int pixelNo, pixel* row) {
  140.     pixel aux;
  141.     int i;
  142.  
  143.     //for each pixel
  144.     for (i = 0; i < pixelNo / 2; i++) {
  145.         //exchange pixel [i] with the pixel at the end minus [i]
  146.         aux = row[i];
  147.         row[i] = row[pixelNo - i - 1];
  148.         row[pixelNo - i - 1] = aux;
  149.     }
  150. }
  151.  
  152. int writeImageHeader(header* h, FILE* fp) {
  153.     //write header fields with newline between them
  154.     if (fprintf(fp, "%s\n%u\n%u\n%u\n", h->type, h->width, h->height, h->depth) < 1) {
  155.         return -1;
  156.     }
  157.  
  158.     return 0;
  159. }
  160.  
  161. int writeRow(int pixelNo, pixel* row, FILE* fp) {
  162.     int i, r;
  163.  
  164.     //for each pixel
  165.     for (i = 0; i < pixelNo; i++) {
  166.         //write one byte per RGB channel in pixel
  167.         r = fwrite(&row[i], sizeof (byte), 3 * sizeof (byte), fp);
  168.  
  169.         //check for errors
  170.         if (r < 1) {
  171.             return -1;
  172.         }
  173.     }
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement