pnmfile.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "image.h"
  23. #include "misc.h"
  24. #define BUF_SIZE 256
  25. class pnm_error { };
  26. static void read_packed(unsigned char *data, int size, std::ifstream &f) {
  27. unsigned char c = 0;
  28. int bitshift = -1;
  29. for (int pos = 0; pos < size; pos++) {
  30. if (bitshift == -1) {
  31. c = f.get();
  32. bitshift = 7;
  33. }
  34. data[pos] = (c >> bitshift) & 1;
  35. bitshift--;
  36. }
  37. }
  38. static void write_packed(unsigned char *data, int size, std::ofstream &f) {
  39. unsigned char c = 0;
  40. int bitshift = 7;
  41. for (int pos = 0; pos < size; pos++) {
  42. c = c | (data[pos] << bitshift);
  43. bitshift--;
  44. if ((bitshift == -1) || (pos == size-1)) {
  45. f.put(c);
  46. bitshift = 7;
  47. c = 0;
  48. }
  49. }
  50. }
  51. /* read PNM field, skipping comments */
  52. static void pnm_read(std::ifstream &file, char *buf) {
  53. char doc[BUF_SIZE];
  54. char c;
  55. file >> c;
  56. while (c == '#') {
  57. file.getline(doc, BUF_SIZE);
  58. file >> c;
  59. }
  60. file.putback(c);
  61. file.width(BUF_SIZE);
  62. file >> buf;
  63. file.ignore();
  64. }
  65. static image<uchar> *loadPBM(const char *name) {
  66. char buf[BUF_SIZE];
  67. /* read header */
  68. std::ifstream file(name, std::ios::in | std::ios::binary);
  69. pnm_read(file, buf);
  70. if (strncmp(buf, "P4", 2))
  71. throw pnm_error();
  72. pnm_read(file, buf);
  73. int width = atoi(buf);
  74. pnm_read(file, buf);
  75. int height = atoi(buf);
  76. /* read data */
  77. image<uchar> *im = new image<uchar>(width, height);
  78. for (int i = 0; i < height; i++)
  79. read_packed(imPtr(im, 0, i), width, file);
  80. return im;
  81. }
  82. static void savePBM(image<uchar> *im, const char *name) {
  83. int width = im->width();
  84. int height = im->height();
  85. std::ofstream file(name, std::ios::out | std::ios::binary);
  86. file << "P4\n" << width << " " << height << "\n";
  87. for (int i = 0; i < height; i++)
  88. write_packed(imPtr(im, 0, i), width, file);
  89. }
  90. static image<uchar> *loadPGM(const char *name) {
  91. char buf[BUF_SIZE];
  92. /* read header */
  93. std::ifstream file(name, std::ios::in | std::ios::binary);
  94. pnm_read(file, buf);
  95. if (strncmp(buf, "P5", 2))
  96. throw pnm_error();
  97. pnm_read(file, buf);
  98. int width = atoi(buf);
  99. pnm_read(file, buf);
  100. int height = atoi(buf);
  101. pnm_read(file, buf);
  102. if (atoi(buf) > UCHAR_MAX)
  103. throw pnm_error();
  104. /* read data */
  105. image<uchar> *im = new image<uchar>(width, height);
  106. file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
  107. return im;
  108. }
  109. static void savePGM(image<uchar> *im, const char *name) {
  110. int width = im->width();
  111. int height = im->height();
  112. std::ofstream file(name, std::ios::out | std::ios::binary);
  113. file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
  114. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
  115. }
  116. static image<rgb> *loadPPM(const char *name) {
  117. char buf[BUF_SIZE], doc[BUF_SIZE];
  118. /* read header */
  119. std::ifstream file(name, std::ios::in | std::ios::binary);
  120. pnm_read(file, buf);
  121. if (strncmp(buf, "P6", 2))
  122. throw pnm_error();
  123. pnm_read(file, buf);
  124. int width = atoi(buf);
  125. pnm_read(file, buf);
  126. int height = atoi(buf);
  127. pnm_read(file, buf);
  128. if (atoi(buf) > UCHAR_MAX)
  129. throw pnm_error();
  130. /* read data */
  131. image<rgb> *im = new image<rgb>(width, height);
  132. file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
  133. return im;
  134. }
  135. static void savePPM(image<rgb> *im, const char *name) {
  136. int width = im->width();
  137. int height = im->height();
  138. std::ofstream file(name, std::ios::out | std::ios::binary);
  139. file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
  140. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
  141. }
  142. template <class T>
  143. void load_image(image<T> **im, const char *name) {
  144. char buf[BUF_SIZE];
  145. /* read header */
  146. std::ifstream file(name, std::ios::in | std::ios::binary);
  147. pnm_read(file, buf);
  148. if (strncmp(buf, "VLIB", 9))
  149. throw pnm_error();
  150. pnm_read(file, buf);
  151. int width = atoi(buf);
  152. pnm_read(file, buf);
  153. int height = atoi(buf);
  154. /* read data */
  155. *im = new image<T>(width, height);
  156. file.read((char *)imPtr((*im), 0, 0), width * height * sizeof(T));
  157. }
  158. template <class T>
  159. void save_image(image<T> *im, const char *name) {
  160. int width = im->width();
  161. int height = im->height();
  162. std::ofstream file(name, std::ios::out | std::ios::binary);
  163. file << "VLIB\n" << width << " " << height << "\n";
  164. file.write((char *)imPtr(im, 0, 0), width * height * sizeof(T));
  165. }
  166. #endif