pnmfile.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* basic image I/O */
  16. #ifndef PNM_FILE_H
  17. #define PNM_FILE_H
  18. #include <cstdlib>
  19. #include <climits>
  20. #include <cstring>
  21. #include <fstream>
  22. #include "segmentation/felzenszwalb/image.h"
  23. #include "segmentation/felzenszwalb/misc.h"
  24. #define BUF_SIZE 256
  25. namespace felzenszwalb{
  26. class pnm_error { };
  27. static void read_packed(unsigned char *data, int size, std::ifstream &f) {
  28. unsigned char c = 0;
  29. int bitshift = -1;
  30. for (int pos = 0; pos < size; pos++) {
  31. if (bitshift == -1) {
  32. c = f.get();
  33. bitshift = 7;
  34. }
  35. data[pos] = (c >> bitshift) & 1;
  36. bitshift--;
  37. }
  38. }
  39. static void write_packed(unsigned char *data, int size, std::ofstream &f) {
  40. unsigned char c = 0;
  41. int bitshift = 7;
  42. for (int pos = 0; pos < size; pos++) {
  43. c = c | (data[pos] << bitshift);
  44. bitshift--;
  45. if ((bitshift == -1) || (pos == size-1)) {
  46. f.put(c);
  47. bitshift = 7;
  48. c = 0;
  49. }
  50. }
  51. }
  52. /* read PNM field, skipping comments */
  53. static void pnm_read(std::ifstream &file, char *buf) {
  54. char doc[BUF_SIZE];
  55. char c;
  56. file >> c;
  57. while (c == '#') {
  58. file.getline(doc, BUF_SIZE);
  59. file >> c;
  60. }
  61. file.putback(c);
  62. file.width(BUF_SIZE);
  63. file >> buf;
  64. file.ignore();
  65. }
  66. static image<uchar> *loadPBM(const char *name) {
  67. char buf[BUF_SIZE];
  68. /* read header */
  69. std::ifstream file(name, std::ios::in | std::ios::binary);
  70. pnm_read(file, buf);
  71. if (strncmp(buf, "P4", 2))
  72. throw pnm_error();
  73. pnm_read(file, buf);
  74. int width = atoi(buf);
  75. pnm_read(file, buf);
  76. int height = atoi(buf);
  77. /* read data */
  78. image<uchar> *im = new image<uchar>(width, height);
  79. for (int i = 0; i < height; i++)
  80. read_packed(imPtr(im, 0, i), width, file);
  81. return im;
  82. }
  83. static void savePBM(image<uchar> *im, const char *name) {
  84. int width = im->width();
  85. int height = im->height();
  86. std::ofstream file(name, std::ios::out | std::ios::binary);
  87. file << "P4\n" << width << " " << height << "\n";
  88. for (int i = 0; i < height; i++)
  89. write_packed(imPtr(im, 0, i), width, file);
  90. }
  91. static image<uchar> *loadPGM(const char *name) {
  92. char buf[BUF_SIZE];
  93. /* read header */
  94. std::ifstream file(name, std::ios::in | std::ios::binary);
  95. pnm_read(file, buf);
  96. if (strncmp(buf, "P5", 2))
  97. throw pnm_error();
  98. pnm_read(file, buf);
  99. int width = atoi(buf);
  100. pnm_read(file, buf);
  101. int height = atoi(buf);
  102. pnm_read(file, buf);
  103. if (atoi(buf) > UCHAR_MAX)
  104. throw pnm_error();
  105. /* read data */
  106. image<uchar> *im = new image<uchar>(width, height);
  107. file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
  108. return im;
  109. }
  110. static void savePGM(image<uchar> *im, const char *name) {
  111. int width = im->width();
  112. int height = im->height();
  113. std::ofstream file(name, std::ios::out | std::ios::binary);
  114. file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
  115. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
  116. }
  117. static image<rgb> *loadPPM(const char *name) {
  118. char buf[BUF_SIZE], doc[BUF_SIZE];
  119. /* read header */
  120. std::ifstream file(name, std::ios::in | std::ios::binary);
  121. pnm_read(file, buf);
  122. if (strncmp(buf, "P6", 2))
  123. throw pnm_error();
  124. pnm_read(file, buf);
  125. int width = atoi(buf);
  126. pnm_read(file, buf);
  127. int height = atoi(buf);
  128. pnm_read(file, buf);
  129. if (atoi(buf) > UCHAR_MAX)
  130. throw pnm_error();
  131. /* read data */
  132. image<rgb> *im = new image<rgb>(width, height);
  133. file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
  134. return im;
  135. }
  136. static void savePPM(image<rgb> *im, const char *name) {
  137. int width = im->width();
  138. int height = im->height();
  139. std::ofstream file(name, std::ios::out | std::ios::binary);
  140. file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
  141. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
  142. }
  143. template <class T>
  144. void load_image(image<T> **im, const char *name) {
  145. char buf[BUF_SIZE];
  146. /* read header */
  147. std::ifstream file(name, std::ios::in | std::ios::binary);
  148. pnm_read(file, buf);
  149. if (strncmp(buf, "VLIB", 9))
  150. throw pnm_error();
  151. pnm_read(file, buf);
  152. int width = atoi(buf);
  153. pnm_read(file, buf);
  154. int height = atoi(buf);
  155. /* read data */
  156. *im = new image<T>(width, height);
  157. file.read((char *)imPtr((*im), 0, 0), width * height * sizeof(T));
  158. }
  159. template <class T>
  160. void save_image(image<T> *im, const char *name) {
  161. int width = im->width();
  162. int height = im->height();
  163. std::ofstream file(name, std::ios::out | std::ios::binary);
  164. file << "VLIB\n" << width << " " << height << "\n";
  165. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(T));
  166. }
  167. }//namespace
  168. #endif