Guest User

Untitled

a guest
Jan 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. using namespace std;
  5. /**
  6. Processes a pixel by forming the negative.
  7. @param blue the blue value of the pixel
  8. @param green the green value of the pixel
  9. @param red the red value of the pixel
  10. */
  11. void process(int& blue, int& green, int& red)
  12. {
  13. blue = 255 - blue;
  14. green = 255 - green;
  15. red = 255 - red;
  16. }
  17. /**
  18. Gets an integer from a binary stream.
  19. @param stream the stream
  20. @param offset the offset at which to read the integer
  21. @return the integer starting at the given offset
  22. */
  23. int get_int(fstream& stream, int offset)
  24. {
  25. stream.seekg(offset);
  26. int result = 0;
  27. int base = 1;
  28. for (int i = 0; i < 4; i++)
  29. {
  30. result = result + stream.get() * base;
  31. base = base * 256;
  32. }
  33. return result;
  34. }
  35. int main()
  36. {
  37. cout << "Please enter the file name: ";
  38. string filename;
  39. cin >> filename;
  40. fstream stream;
  41.  
  42. // Open as a binary file
  43. stream.open(filename.c_str(),
  44. ios::in|ios::out|ios::binary);
  45.  
  46. // Get the image dimensions
  47. int file_size = get_int(stream, 2);
  48. int start = get_int(stream, 10);
  49. int width = get_int(stream, 18);
  50. int height = get_int(stream, 22);
  51. // Scan lines must occupy multiples of four bytes
  52. int scanline_size = width * 3;
  53. int padding = 0;
  54. if (scanline_size % 4 != 0)
  55. {
  56. padding = 4 - scanline_size % 4;
  57. }
  58. if (file_size != start +
  59. (scanline_size + padding) * height)
  60. {
  61. cout << "Not a 24-bit true color image file.“
  62. << endl;
  63. return 1;
  64. }
  65. // Go to the start of the pixels
  66. stream.seekg(start);
  67.  
  68. // For each scan line
  69. for (int i = 0; i < height; i++)
  70. {
  71. // For each pixel
  72. for (int j = 0; j < width; j++)
  73. {
  74. // Go to the start of the pixel
  75. int pos = stream.tellg();
  76.  
  77. // Read the pixel
  78. int blue = stream.get();
  79. int green = stream.get();
  80. int red = stream.get();
  81. // Process the pixel
  82. process(blue, green, red);
  83.  
  84. // Go back to the start of the pixel
  85. stream.seekp(pos);
  86. stream.put(blue);
  87.  
  88. // Write the pixel
  89. stream.put(green);
  90. stream.put(red);
  91. }
  92.  
  93. // Skip the padding
  94. stream.seekg(padding, ios::cur);
  95. }
  96. return 0;
  97. }
Add Comment
Please, Sign In to add comment