rawls.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. bool rawls::saveAsRAWLS(unsigned width, unsigned height, unsigned nbChanels, std::string comments, float* buffer, std::string outfileName){
  86. std::ofstream outfile(outfileName, std::ios::out | std::ios::binary);
  87. outfile << "IHDR" << std::endl;
  88. outfile << ((sizeof(width) + sizeof(height) + sizeof(nbChanels))) << std::endl;
  89. outfile.write((char *) &width, sizeof(width));
  90. outfile << " ";
  91. outfile.write((char *) &height, sizeof(height));
  92. outfile << " ";
  93. outfile.write((char *) &nbChanels, sizeof(nbChanels));
  94. outfile << std::endl;
  95. // Part 2
  96. // Comments (usefull information about scene and generation data used)
  97. outfile << "COMMENTS" << std::endl;
  98. outfile << comments;
  99. // Part 3
  100. // using data information write specific chunck
  101. outfile << "DATA" << std::endl;
  102. outfile << (sizeof(float) * nbChanels * width * height) << std::endl;
  103. for (int y = 0; y < height; ++y) {
  104. for (int x = 0; x < width; ++x) {
  105. float pixel[3];
  106. pixel[0] = buffer[3 * (y * width + x) + 0];
  107. pixel[1] = buffer[3 * (y * width + x) + 1];
  108. pixel[2] = buffer[3 * (y * width + x) + 2];
  109. outfile.write((char *) &pixel[0], sizeof(pixel[0]));
  110. outfile.write((char *) &pixel[1], sizeof(pixel[1]));
  111. outfile.write((char *) &pixel[2], sizeof(pixel[2]));
  112. outfile << std::endl;
  113. }
  114. }
  115. outfile.close();
  116. if(!outfile.good()) {
  117. std::cout << "Error occurred at writing time!" << std::endl;
  118. return false;
  119. }
  120. std::cout << "Image is now saved as .rawls into " << outfileName << std::endl;
  121. return true;
  122. }
  123. float* rawls::getPixelsRAWLS(std::string filename){
  124. // get dimensions information from image
  125. unsigned width, height, nbChanels;
  126. std::tuple<unsigned, unsigned, unsigned> dimensions = getDimensionsRAWLS(filename);
  127. width = std::get<0>(dimensions);
  128. height = std::get<1>(dimensions);
  129. nbChanels = std::get<2>(dimensions);
  130. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  131. if(!rf) {
  132. std::cout << "Cannot open file!" << std::endl;
  133. }
  134. std::string line;
  135. char c; // to get space of new line char
  136. bool dataBegin = false;
  137. // READ DATA info
  138. // case of data chunck begin
  139. while (!dataBegin && std::getline(rf, line)) {
  140. if (line.find(std::string("DATA")) != std::string::npos){
  141. dataBegin = true;
  142. }
  143. }
  144. // resize buffer if necessary
  145. float* buffer = new float[height * width * nbChanels];
  146. std::getline(rf, line);
  147. unsigned size = std::stoi(line);
  148. float chanelValue;
  149. for(unsigned y = 0; y < height; y++){
  150. for(unsigned x = 0; x < width; x++) {
  151. for(int j = 0; j < nbChanels; j++){
  152. rf.read((char *) &chanelValue, sizeof(float));
  153. buffer[nbChanels * width * y + nbChanels * x + j] = chanelValue;
  154. }
  155. // go to next line
  156. rf.get(c);
  157. }
  158. }
  159. rf.close();
  160. if(!rf.good()) {
  161. std::cout << "Error occurred at writing time!" << std::endl;
  162. }
  163. return buffer;
  164. }
  165. std::tuple<unsigned, unsigned, unsigned> rawls::getDimensionsRAWLS(std::string filename){
  166. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  167. if(!rf) {
  168. std::cout << "Cannot open file!" << std::endl;
  169. }
  170. std::string line;
  171. unsigned nbChanels, width, height;
  172. char c; // to get space of new line char
  173. // READ IHDR info
  174. bool ihdrBegin = false;
  175. while (!ihdrBegin && std::getline(rf, line)) {
  176. if (line.find(std::string("IHDR")) != std::string::npos){
  177. ihdrBegin = true;
  178. std::getline(rf, line); // avoid data size line
  179. rf.read((char *) &width, sizeof(unsigned));
  180. rf.get(c);
  181. rf.read((char *) &height, sizeof(unsigned));
  182. rf.get(c);
  183. rf.read((char *) &nbChanels, sizeof(unsigned));
  184. rf.get(c);
  185. }
  186. }
  187. rf.close();
  188. if(!rf.good()) {
  189. std::cout << "Error occurred at writing time!" << std::endl;
  190. }
  191. return std::make_tuple(width, height, nbChanels);
  192. }
  193. std::tuple<unsigned, unsigned, unsigned, float*> rawls::getDataRAWLS(std::string filename){
  194. // only one read buffer used for the whole function
  195. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  196. if(!rf) {
  197. std::cout << "Cannot open file!" << std::endl;
  198. }
  199. std::string line;
  200. unsigned nbChanels, width, height;
  201. char c; // to get space of new line char
  202. // READ IHDR info
  203. bool ihdrBegin = false;
  204. while (!ihdrBegin && std::getline(rf, line)) {
  205. if (line.find(std::string("IHDR")) != std::string::npos){
  206. ihdrBegin = true;
  207. std::getline(rf, line); // avoid data size line
  208. rf.read((char *) &width, sizeof(unsigned));
  209. rf.get(c);
  210. rf.read((char *) &height, sizeof(unsigned));
  211. rf.get(c);
  212. rf.read((char *) &nbChanels, sizeof(unsigned));
  213. rf.get(c);
  214. }
  215. }
  216. bool dataBegin = false;
  217. // READ DATA info
  218. // case of data chunck begin
  219. while (!dataBegin && std::getline(rf, line)) {
  220. if (line.find(std::string("DATA")) != std::string::npos){
  221. dataBegin = true;
  222. }
  223. }
  224. // resize buffer if necessary
  225. float* buffer = new float[height * width * nbChanels];
  226. std::getline(rf, line);
  227. unsigned size = std::stoi(line);
  228. float chanelValue;
  229. for(unsigned y = 0; y < height; y++){
  230. for(unsigned x = 0; x < width; x++) {
  231. for(int j = 0; j < nbChanels; j++){
  232. rf.read((char *) &chanelValue, sizeof(float));
  233. buffer[nbChanels * width * y + nbChanels * x + j] = chanelValue;
  234. }
  235. // go to next line
  236. rf.get(c);
  237. }
  238. }
  239. rf.close();
  240. if(!rf.good()) {
  241. std::cout << "Error occurred at writing time!" << std::endl;
  242. }
  243. return std::make_tuple(width, height, nbChanels, buffer);
  244. }
  245. std::string rawls::getCommentsRAWLS(std::string filename){
  246. std::string comments;
  247. // only one read buffer used for the whole function
  248. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  249. if(!rf) {
  250. std::cout << "Cannot open file!" << std::endl;
  251. }
  252. std::string line;
  253. unsigned nbChanels, width, height;
  254. char c; // to get space of new line char
  255. // READ IHDR info
  256. bool commentsBegin = false;
  257. while (!commentsBegin && std::getline(rf, line)) {
  258. if (line.find(std::string("COMMENTS")) != std::string::npos){
  259. commentsBegin = true;
  260. }
  261. }
  262. bool dataBegin = false;
  263. // READ DATA info
  264. // case of data chunck begin
  265. while (!dataBegin && std::getline(rf, line)) {
  266. if (line.find(std::string("DATA")) != std::string::npos){
  267. dataBegin = true;
  268. }
  269. else
  270. {
  271. comments = comments + line + '\n';
  272. }
  273. }
  274. return comments;
  275. }