You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pnm.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "pnm.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "environment/Environment.h"
  7. // pnm
  8. // The PNM format is just an abstraction of the PBM, PGM, and PPM formats. I.e. the name "PNM" refers collectively to PBM, PGM, and PPM.
  9. // pam has the advanced header
  10. RGBAPNMObject * readPPM(const char* fileName) {
  11. RGBAPNMObject *data=new RGBAPNMObject;
  12. // open the file to read just the header reading
  13. FILE* fr = fopen(std::string(Environment::getResourceDir() + "/" + fileName).c_str(), "r");
  14. if (!fr) {
  15. std::cout << "Can't open " << fileName << std::endl;
  16. return nullptr;
  17. }
  18. // formatted read of header
  19. char fileType[11];
  20. int res=0;
  21. res = fscanf(fr, "%9s", fileType);
  22. data->magicNum=fileType;
  23. //std::cout << "fileType:" << fileType << std::endl;
  24. unsigned char bits = 0;
  25. // check to see if it's a PPM image file
  26. if (strncmp(fileType, "P4" , 2) == 0) {
  27. bits = 1; // PBM
  28. } else if (strncmp(fileType, "P5" , 2) == 0) {
  29. bits = 8; // PGM
  30. } else if (strncmp(fileType, "P6" , 2) == 0) {
  31. bits = 24; // PPM
  32. } else if (strncmp(fileType, "P8" , 2) == 0) {
  33. bits = 32; // PPM
  34. }
  35. //std::cout << "bits:" << bits << std::endl;
  36. // read the rest of header
  37. unsigned int width, height;
  38. res = fscanf(fr, "%u\n %u\n", &width, &height);
  39. data->width = width;
  40. data->height = height;
  41. // read maximum
  42. data->maxColVal = 255; // just set a default
  43. if (bits != 1) {
  44. unsigned int maximum;
  45. res = fscanf(fr, "%u\n", &maximum);
  46. data->maxColVal = height;
  47. }
  48. unsigned int size = width * height;
  49. if (bits != 1) {
  50. size *= bits/8;
  51. }
  52. // allocate array for pixels
  53. char* pixels = new char[size];
  54. // unformatted read of binary pixel data
  55. res = fread(pixels, sizeof(int), size, fr);
  56. data->m_Ptr = pixels;
  57. // close file
  58. fclose(fr);
  59. // return the array
  60. return data;
  61. } // end of readPPM
  62. void writePBM4(const char *filename, const RGBAPNMObject &data) {
  63. FILE *fp = fopen(filename, "wb"); /* b - binary mode */
  64. fprintf(fp, "P6\n%d %d\n255\n", data.width, data.height);
  65. for (unsigned int j = 0; j < data.height; ++j) {
  66. for (unsigned int i = 0; i < data.width; ++i) {
  67. static unsigned char color[1];
  68. unsigned int pos = ((i * 4) + (j * 4 * 1024));
  69. //color[0] = data.m_Ptr[pos + 0]; /* red */
  70. //color[1] = data.m_Ptr[pos + 1]; /* green */
  71. //color[2] = data.m_Ptr[pos + 2]; /* blue */
  72. color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 3]); /*
  73. alpha */
  74. fwrite(color, 1, 1, fp);
  75. }
  76. }
  77. fclose(fp);
  78. }
  79. void writePGM5(const char *filename, const RGBAPNMObject &data) {
  80. FILE *fp = fopen(filename, "wb"); /* b - binary mode */
  81. fprintf(fp, "P5\n%d %d\n255\n", data.width, data.height);
  82. for (unsigned int j = 0; j < data.height; ++j) {
  83. for (unsigned int i = 0; i < data.width; ++i) {
  84. static unsigned char color[1];
  85. unsigned int pos = ((i * 4) + (j * 4 * 1024));
  86. color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 0]); /* red
  87. */
  88. //color[1] = data.m_Ptr[pos + 1]; /* green */
  89. //color[2] = data.m_Ptr[pos + 2]; /* blue */
  90. //color[3] = data.m_Ptr[pos + 3]; /* alpha */
  91. fwrite(color, 1, 1, fp);
  92. }
  93. }
  94. fclose(fp);
  95. }
  96. void writePPM6(const char *filename, const RGBAPNMObject &data) {
  97. FILE *fp = fopen(filename, "wb"); /* b - binary mode */
  98. fprintf(fp, "P6\n%d %d\n255\n", data.width, data.height);
  99. for (unsigned int j = 0; j < data.height; ++j) {
  100. for (unsigned int i = 0; i < data.width; ++i) {
  101. static unsigned char color[3];
  102. unsigned int pos = ((i * 4) + (j * 4 * 1024));
  103. color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 0]); /* red */
  104. color[1] = static_cast<unsigned char>(data.m_Ptr[pos + 1]); /* green */
  105. color[2] = static_cast<unsigned char>(data.m_Ptr[pos + 2]); /* blue */
  106. //color[3] = data.m_Ptr[pos + 3]; /* alpha */
  107. fwrite(color, 1, 3, fp);
  108. }
  109. }
  110. fclose(fp);
  111. }
  112. void writePPM8(const char *filename, const RGBAPNMObject &data) {
  113. FILE *fp = fopen(filename, "wb"); /* b - binary mode */
  114. fprintf(fp, "P8\n%d %d\n255\n", data.width, data.height);
  115. for (unsigned int j = 0; j < data.height; ++j) {
  116. for (unsigned int i = 0; i < data.width; ++i) {
  117. static unsigned char color[4];
  118. unsigned int pos = ((i * 4) + (j * 4 * 1024));
  119. color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 0]); /* red */
  120. color[1] = static_cast<unsigned char>(data.m_Ptr[pos + 1]); /* green */
  121. color[2] = static_cast<unsigned char>(data.m_Ptr[pos + 2]); /* blue */
  122. color[3] = static_cast<unsigned char>(data.m_Ptr[pos + 3]); /* alpha */
  123. fwrite(color, 1, 4, fp);
  124. }
  125. }
  126. fclose(fp);
  127. }