rawls.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // 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::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.write((char *) &height, sizeof(height));
  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. }
  112. outfile << std::endl;
  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. }
  155. // go to next line
  156. rf.get(c);
  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.read((char *) &height, sizeof(unsigned));
  180. rf.read((char *) &nbChanels, sizeof(unsigned));
  181. rf.get(c);
  182. }
  183. }
  184. rf.close();
  185. if(!rf.good()) {
  186. std::cout << "Error occurred at writing time!" << std::endl;
  187. }
  188. return std::make_tuple(width, height, nbChanels);
  189. }
  190. std::tuple<unsigned, unsigned, unsigned, float*> rawls::getDataRAWLS(std::string filename){
  191. // only one read buffer used for the whole function
  192. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  193. if(!rf) {
  194. std::cout << "Cannot open file!" << std::endl;
  195. }
  196. std::string line;
  197. unsigned nbChanels, width, height;
  198. char c; // to get space of new line char
  199. // READ IHDR info
  200. bool ihdrBegin = false;
  201. while (!ihdrBegin && std::getline(rf, line)) {
  202. if (line.find(std::string("IHDR")) != std::string::npos){
  203. ihdrBegin = true;
  204. std::getline(rf, line); // avoid data size line
  205. rf.read((char *) &width, sizeof(unsigned));
  206. rf.read((char *) &height, sizeof(unsigned));
  207. rf.read((char *) &nbChanels, sizeof(unsigned));
  208. }
  209. }
  210. bool dataBegin = false;
  211. // READ DATA info
  212. // case of data chunck begin
  213. while (!dataBegin && std::getline(rf, line)) {
  214. if (line.find(std::string("DATA")) != std::string::npos){
  215. dataBegin = true;
  216. }
  217. }
  218. // resize buffer if necessary
  219. float* buffer = new float[height * width * nbChanels];
  220. std::getline(rf, line);
  221. unsigned size = std::stoi(line);
  222. float chanelValue;
  223. for(unsigned y = 0; y < height; y++){
  224. for(unsigned x = 0; x < width; x++) {
  225. for(int j = 0; j < nbChanels; j++){
  226. rf.read((char *) &chanelValue, sizeof(float));
  227. buffer[nbChanels * width * y + nbChanels * x + j] = chanelValue;
  228. }
  229. }
  230. // go to next line
  231. rf.get(c);
  232. }
  233. rf.close();
  234. if(!rf.good()) {
  235. std::cout << "Error occurred at writing time!" << std::endl;
  236. }
  237. return std::make_tuple(width, height, nbChanels, buffer);
  238. }
  239. std::string rawls::getCommentsRAWLS(std::string filename){
  240. std::string comments;
  241. // only one read buffer used for the whole function
  242. std::ifstream rf(filename, std::ios::out | std::ios::binary);
  243. if(!rf) {
  244. std::cout << "Cannot open file!" << std::endl;
  245. }
  246. std::string line;
  247. unsigned nbChanels, width, height;
  248. char c; // to get space of new line char
  249. // READ IHDR info
  250. bool commentsBegin = false;
  251. while (!commentsBegin && std::getline(rf, line)) {
  252. if (line.find(std::string("COMMENTS")) != std::string::npos){
  253. commentsBegin = true;
  254. }
  255. }
  256. bool dataBegin = false;
  257. // READ DATA info
  258. // case of data chunck begin
  259. while (!dataBegin && std::getline(rf, line)) {
  260. if (line.find(std::string("DATA")) != std::string::npos){
  261. dataBegin = true;
  262. }
  263. else
  264. {
  265. comments = comments + line + '\n';
  266. }
  267. }
  268. return comments;
  269. }