Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

pgm header

By: Tiana9875 on Nov 27th, 2012  |  syntax: C  |  size: 3.20 KB  |  views: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #ifndef _PGM_H_
  2. #define _PGM_H_
  3.  
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. #define PGM_MAGIC "P5"
  8.  
  9. #ifdef _WIN32
  10. #define STRTOK_R(ptr, del, saveptr) strtok_s(ptr, del, saveptr)
  11. #else
  12. #define STRTOK_R(ptr, del, saveptr) strtok_r(ptr, del, saveptr)
  13. #endif
  14.  
  15. typedef struct _pgm_t {
  16. int width;
  17. int height;
  18. unsigned char *buf;
  19. } pgm_t;
  20.  
  21. int readPGM(pgm_t* pgm, const char* filename)
  22. {
  23. char *token, *pc, *saveptr;
  24. char *buf;
  25. size_t bufsize;
  26. char del[] = " \t\n";
  27. unsigned char *dot;
  28.  
  29. long begin, end;
  30. int filesize;
  31. int i, w, h, luma, pixs;
  32.  
  33.  
  34. FILE* fp;
  35. if ((fp = fopen(filename, "rb"))==NULL) {
  36. fprintf(stderr, "Failed to open file\n");
  37. return -1;
  38. }
  39.  
  40. fseek(fp, 0, SEEK_SET);
  41. begin = ftell(fp);
  42. fseek(fp, 0, SEEK_END);
  43. end = ftell(fp);
  44. filesize = (int)(end - begin);
  45.  
  46. buf = (char*)malloc(filesize * sizeof(char));
  47. fseek(fp, 0, SEEK_SET);
  48. bufsize = fread(buf, filesize * sizeof(char), 1, fp);
  49.  
  50. fclose(fp);
  51.  
  52. token = (char *)STRTOK_R(buf, del, &saveptr);
  53. if (strncmp(token, PGM_MAGIC, 2) != 0) {
  54. return -1;
  55. }
  56.  
  57. token = (char *)STRTOK_R(NULL, del, &saveptr);
  58. if (token[0] == '#' ) {
  59. token = (char *)STRTOK_R(NULL, "\n", &saveptr);
  60. token = (char *)STRTOK_R(NULL, del, &saveptr);
  61. }
  62.  
  63. w = strtoul(token, &pc, 10);
  64. token = (char *)STRTOK_R(NULL, del, &saveptr);
  65. h = strtoul(token, &pc, 10);
  66. token = (char *)STRTOK_R(NULL, del, &saveptr);
  67. luma = strtoul(token, &pc, 10);
  68.  
  69. token = pc + 1;
  70. pixs = w * h;
  71.  
  72. pgm->buf = (unsigned char *)malloc(pixs * sizeof(unsigned char));
  73.  
  74. dot = pgm->buf;
  75.  
  76. for (i=0; i< pixs; i++, dot++) {
  77. *dot = *token++;
  78. }
  79.  
  80. pgm->width = w;
  81. pgm->height = h;
  82.  
  83. return 0;
  84. }
  85.  
  86. int writePGM(pgm_t* pgm, const char* filename)
  87. {
  88. int i, w, h, pixs;
  89. FILE* fp;
  90. unsigned char* dot;
  91.  
  92. w = pgm->width;
  93. h = pgm->height;
  94. pixs = w * h;
  95.  
  96. if ((fp = fopen(filename, "wb+")) ==NULL) {
  97. fprintf(stderr, "Failed to open file\n");
  98. return -1;
  99. }
  100.  
  101. fprintf (fp, "%s\n%d %d\n255\n", PGM_MAGIC, w, h);
  102.  
  103. dot = pgm->buf;
  104.  
  105. for (i=0; i<pixs; i++, dot++) {
  106. putc((unsigned char)*dot, fp);
  107. }
  108.  
  109. fclose(fp);
  110.  
  111. return 0;
  112. }
  113.  
  114. int normalizeD2PGM(pgm_t* pgm, double* x)
  115. {
  116. int i, j, w, h;
  117.  
  118. w = pgm->width;
  119. h = pgm->height;
  120.  
  121. pgm->buf = (unsigned char*)malloc(w * h * sizeof(unsigned char));
  122.  
  123. double min = 0;
  124. double max = 0;
  125. for (i=0; i < h; i++) {
  126. for (j=0; j < w; j++) {
  127. if (max < x[i*w+j])
  128. max = x[i*w+j];
  129. if (min > x[i*w+j])
  130. min = x[i*w+j];
  131. }
  132. }
  133.  
  134. for (i=0; i < h; i++) {
  135. for (j=0; j < w; j++) {
  136. if((max-min)!=0)
  137. pgm->buf[i*w+j] = (unsigned char)(255*(x[i*w+j]-min)/(max-min));
  138. else
  139. pgm->buf[i*w+j]= 0;
  140. }
  141. }
  142.  
  143. return 0;
  144. }
  145.  
  146. int normalizeF2PGM(pgm_t* pgm, float* x)
  147. {
  148. int i, j, w, h;
  149.  
  150. w = pgm->width;
  151. h = pgm->height;
  152.  
  153. pgm->buf = (unsigned char*)malloc(w * h * sizeof(unsigned char));
  154.  
  155. float min = 0;
  156. float max = 0;
  157. for (i=0; i < h; i++) {
  158. for (j=0; j < w; j++) {
  159. if (max < x[i*w+j])
  160. max = x[i*w+j];
  161. if (min > x[i*w+j])
  162. min = x[i*w+j];
  163. }
  164. }
  165.  
  166. for (i=0; i < h; i++) {
  167. for (j=0; j < w; j++) {
  168. if((max-min)!=0)
  169. pgm->buf[i*w+j] = (unsigned char)(255*(x[i*w+j]-min)/(max-min));
  170. else
  171. pgm->buf[i*w+j]= 0;
  172. }
  173. }
  174.  
  175. return 0;
  176. }
  177.  
  178. int destroyPGM(pgm_t* pgm)
  179. {
  180. if (pgm->buf) {
  181. free(pgm->buf);
  182. }
  183.  
  184. return 0;
  185. }
  186.  
  187. #endif /* _PGM_H_ */