rawls_h_flip.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <math.h>
  7. #include <bitset>
  8. int main(int argc, char *argv[]){
  9. std::string imageName;
  10. std::string outfileName;
  11. for (int i = 1; i < argc; ++i) {
  12. if (!strcmp(argv[i], "--image") || !strcmp(argv[i], "-image")) {
  13. imageName = argv[++i];
  14. } else if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-outfile")) {
  15. outfileName = argv[++i];
  16. }
  17. }
  18. std::cout << "Read image `" << imageName << "` and save it into " << outfileName << std::endl;
  19. std::ifstream rf(imageName, std::ios::out | std::ios::binary);
  20. if(!rf) {
  21. std::cout << "Cannot open file!" << std::endl;
  22. return 1;
  23. }
  24. std::string line;
  25. int nbChanels, width, height;
  26. char c; // to get space of new line char
  27. // READ IHDR info
  28. bool ihdrBegin = false;
  29. while (!ihdrBegin && std::getline(rf, line)) {
  30. if (line.find(std::string("IHDR")) != std::string::npos){
  31. ihdrBegin = true;
  32. std::getline(rf, line); // avoid data size line
  33. rf.read((char *) &width, sizeof(int));
  34. rf.get(c);
  35. rf.read((char *) &height, sizeof(int));
  36. rf.get(c);
  37. rf.read((char *) &nbChanels, sizeof(int));
  38. rf.get(c);
  39. }
  40. }
  41. bool dataBegin = false;
  42. // READ DATA info
  43. // case of data chunck begin
  44. while (!dataBegin && std::getline(rf, line)) {
  45. if (line.find(std::string("DATA")) != std::string::npos){
  46. dataBegin = true;
  47. }
  48. }
  49. std::getline(rf, line);
  50. int size = std::stoi(line);
  51. // compute number of pixels into image
  52. int nbPixels = (size / (nbChanels * 4));
  53. // create outfile
  54. // now write image as .ppm
  55. std::ofstream outfile(outfileName);
  56. outfile << "P" << nbChanels << std::endl;
  57. outfile << width << " " << height << std::endl;
  58. outfile << "255" << std::endl;
  59. for (int i = 0; i < height; i++){
  60. float widthValues[width][nbChanels];
  61. for (int j = 0; j < width; j++){
  62. // for each chanel read and keep save float value into new file
  63. for(int k = 0; k < nbChanels; k++){
  64. rf.read((char *) &widthValues[j][k], sizeof(float));
  65. }
  66. // go to next line
  67. rf.get(c);
  68. }
  69. for (int j = width; j > 0; j--){
  70. for(int k = 0; k < nbChanels; k++){
  71. outfile << int(widthValues[j][k]) << " ";
  72. }
  73. }
  74. outfile << std::endl;
  75. }
  76. rf.close();
  77. if(!rf.good()) {
  78. std::cout << "Error occurred at reading time!" << std::endl;
  79. return 1;
  80. }
  81. std::cout << "Image is now flipped and saved as .ppm into " << outfileName << std::endl;
  82. outfile.close();
  83. }