rawls_v1.cpp 10 KB

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