rawls.cpp 10 KB

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