rawls.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "rawls.h"
  2. #include <fstream>
  3. #include <memory>
  4. #include "lodepng.h"
  5. /*
  6. * Read all pixels values from .rawls image file and save as PPM image
  7. */
  8. bool rawls::convertToPPM(std::string imageName, std::string outfileName){
  9. // get dimensions and data information from image
  10. unsigned width, height, nbChanels;
  11. float* buffer;
  12. std::tuple<unsigned, unsigned, unsigned, float*> data = getDataRAWLS(imageName);
  13. width = std::get<0>(data);
  14. height = std::get<1>(data);
  15. nbChanels = std::get<2>(data);
  16. buffer = std::get<3>(data);
  17. return saveAsPPM(width, height, nbChanels, buffer, outfileName);
  18. }
  19. /*
  20. * Read all pixels values from .rawls image file and save as PNG image
  21. */
  22. bool rawls::convertToPNG(std::string imageName, std::string outfileName){
  23. // get dimensions and data information from image
  24. unsigned width, height, nbChanels;
  25. float* buffer;
  26. std::tuple<unsigned, unsigned, unsigned, float*> data = getDataRAWLS(imageName);
  27. width = std::get<0>(data);
  28. height = std::get<1>(data);
  29. nbChanels = std::get<2>(data);
  30. buffer = std::get<3>(data);
  31. return saveAsPNG(width, height, nbChanels, buffer, outfileName);
  32. }
  33. /*
  34. * Convert buffer using dimensions to PPM image format
  35. */
  36. bool rawls::saveAsPPM(unsigned width, unsigned height, unsigned nbChanels, float* buffer, std::string outfileName){
  37. // open buffer
  38. std::ofstream outfile(outfileName);
  39. outfile << "P" << nbChanels << std::endl;
  40. outfile << width << " " << height << std::endl;
  41. outfile << "255" << std::endl;
  42. for(unsigned y = 0; y < height; y++){
  43. for(unsigned x = 0; x < width; x++) {
  44. // for each chanel read and keep save float value i nto new file
  45. for(int j = 0; j < nbChanels; j++){
  46. outfile << int(GammaConvert(buffer[nbChanels * width * y + nbChanels * x + j])) << " ";
  47. }
  48. }
  49. outfile << std::endl;
  50. }
  51. std::cout << "Image is now saved as .ppm into " << outfileName << std::endl;
  52. outfile.close();
  53. if(!outfile.good()) {
  54. std::cout << "Error occurred at writing time!" << std::endl;
  55. return false;
  56. }
  57. return true;
  58. }
  59. /*
  60. * Convert buffer using dimensions to PNG image format
  61. */
  62. bool rawls::saveAsPNG(unsigned width, unsigned height, unsigned nbChanels, float* buffer, std::string outfileName){
  63. // image buffer
  64. unsigned nbChanelsAlpha = nbChanels + 1;
  65. std::unique_ptr<uint8_t[]> outBuf(new uint8_t[width * height * nbChanelsAlpha]);
  66. uint8_t *image = outBuf.get();
  67. for(unsigned y = 0; y < height; y++){
  68. for(unsigned x = 0; x < width; x++) {
  69. for(int j = 0; j < nbChanels; j++){
  70. float value = buffer[nbChanels * width * y + nbChanels * x + j];
  71. image[nbChanelsAlpha * width * y + nbChanelsAlpha * x + j] = int(GammaConvert(value));
  72. }
  73. image[nbChanelsAlpha * width * y + nbChanelsAlpha * x + 3] = 255;
  74. }
  75. }
  76. unsigned error = lodepng::encode(outfileName, image, width, height);
  77. //if there's an error, display it
  78. if(error) {
  79. std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
  80. return false;
  81. }
  82. std::cout << "Image is now saved as .png into " << outfileName << std::endl;
  83. return true;
  84. }
  85. float* rawls::getPixelsRAWLS(std::string filename){
  86. // get dimensions information from image
  87. unsigned width, height, nbChanels;
  88. std::tuple<unsigned, unsigned, unsigned> dimensions = getDimensionsRAWLS(filename);
  89. width = std::get<0>(dimensions);
  90. height = std::get<1>(dimensions);
  91. nbChanels = std::get<2>(dimensions);
  92. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  93. if(!rf) {
  94. std::cout << "Cannot open file!" << std::endl;
  95. }
  96. std::string line;
  97. char c; // to get space of new line char
  98. bool dataBegin = false;
  99. // READ DATA info
  100. // case of data chunck begin
  101. while (!dataBegin && std::getline(rf, line)) {
  102. if (line.find(std::string("DATA")) != std::string::npos){
  103. dataBegin = true;
  104. }
  105. }
  106. // resize buffer if necessary
  107. float* buffer = new float[height * width * nbChanels];
  108. std::getline(rf, line);
  109. unsigned size = std::stoi(line);
  110. float chanelValue;
  111. for(unsigned y = 0; y < height; y++){
  112. for(unsigned x = 0; x < width; x++) {
  113. for(int j = 0; j < nbChanels; j++){
  114. rf.read((char *) &chanelValue, sizeof(float));
  115. buffer[nbChanels * width * y + nbChanels * x + j] = chanelValue;
  116. }
  117. // go to next line
  118. rf.get(c);
  119. }
  120. }
  121. rf.close();
  122. if(!rf.good()) {
  123. std::cout << "Error occurred at writing time!" << std::endl;
  124. }
  125. return buffer;
  126. }
  127. std::tuple<unsigned, unsigned, unsigned> rawls::getDimensionsRAWLS(std::string filename){
  128. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  129. if(!rf) {
  130. std::cout << "Cannot open file!" << std::endl;
  131. }
  132. std::string line;
  133. unsigned nbChanels, width, height;
  134. char c; // to get space of new line char
  135. // READ IHDR info
  136. bool ihdrBegin = false;
  137. while (!ihdrBegin && std::getline(rf, line)) {
  138. if (line.find(std::string("IHDR")) != std::string::npos){
  139. ihdrBegin = true;
  140. std::getline(rf, line); // avoid data size line
  141. rf.read((char *) &width, sizeof(unsigned));
  142. rf.get(c);
  143. rf.read((char *) &height, sizeof(unsigned));
  144. rf.get(c);
  145. rf.read((char *) &nbChanels, sizeof(unsigned));
  146. rf.get(c);
  147. }
  148. }
  149. rf.close();
  150. if(!rf.good()) {
  151. std::cout << "Error occurred at writing time!" << std::endl;
  152. }
  153. return std::make_tuple(width, height, nbChanels);
  154. }
  155. std::tuple<unsigned, unsigned, unsigned, float*> rawls::getDataRAWLS(std::string filename){
  156. // only one read buffer used for the whole function
  157. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  158. if(!rf) {
  159. std::cout << "Cannot open file!" << std::endl;
  160. }
  161. std::string line;
  162. unsigned nbChanels, width, height;
  163. char c; // to get space of new line char
  164. // READ IHDR info
  165. bool ihdrBegin = false;
  166. while (!ihdrBegin && std::getline(rf, line)) {
  167. if (line.find(std::string("IHDR")) != std::string::npos){
  168. ihdrBegin = true;
  169. std::getline(rf, line); // avoid data size line
  170. rf.read((char *) &width, sizeof(unsigned));
  171. rf.get(c);
  172. rf.read((char *) &height, sizeof(unsigned));
  173. rf.get(c);
  174. rf.read((char *) &nbChanels, sizeof(unsigned));
  175. rf.get(c);
  176. }
  177. }
  178. bool dataBegin = false;
  179. // READ DATA info
  180. // case of data chunck begin
  181. while (!dataBegin && std::getline(rf, line)) {
  182. if (line.find(std::string("DATA")) != std::string::npos){
  183. dataBegin = true;
  184. }
  185. }
  186. // resize buffer if necessary
  187. float* buffer = new float[height * width * nbChanels];
  188. std::getline(rf, line);
  189. unsigned size = std::stoi(line);
  190. float chanelValue;
  191. for(unsigned y = 0; y < height; y++){
  192. for(unsigned x = 0; x < width; x++) {
  193. for(int j = 0; j < nbChanels; j++){
  194. rf.read((char *) &chanelValue, sizeof(float));
  195. buffer[nbChanels * width * y + nbChanels * x + j] = chanelValue;
  196. }
  197. // go to next line
  198. rf.get(c);
  199. }
  200. }
  201. rf.close();
  202. if(!rf.good()) {
  203. std::cout << "Error occurred at writing time!" << std::endl;
  204. }
  205. return std::make_tuple(width, height, nbChanels, buffer);
  206. }