You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitmap.cpp 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: bitmap.cpp
  5. *
  6. * Description: Bitmap
  7. *
  8. * Version: 1.0
  9. * Created: 04/02/2014 07:21:51 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "bitmap.h"
  19. #include "stb_image.h"
  20. namespace vtk {
  21. Bitmap::Bitmap() {
  22. forceChannels = 4;
  23. }
  24. bool Bitmap::loadFromFile(const std::string& fileName) {
  25. imageData = stbi_load(fileName.c_str(), &x, &y, &n, forceChannels);
  26. if (!imageData) return false;
  27. flipVertical();
  28. return true;
  29. }
  30. unsigned char* Bitmap::getPixelDataPtr() {
  31. return imageData;
  32. }
  33. void Bitmap::flipVertical() {
  34. int widthBytes = x * 4;
  35. unsigned char* top = NULL;
  36. unsigned char* bottom = NULL;
  37. unsigned char temp = 0;
  38. int halfHeight = y/2;
  39. for (int row = 0; row < halfHeight; row++) {
  40. top = imageData + row * widthBytes;
  41. bottom = imageData + (y - row - 1) * widthBytes;
  42. for (int col = 0; col < widthBytes; col++) {
  43. temp = *top;
  44. *top = *bottom;
  45. *bottom = temp;
  46. top++;
  47. bottom++;
  48. }
  49. }
  50. }
  51. int Bitmap::getHeight() { return y; }
  52. int Bitmap::getWidth() { return x; }
  53. int Bitmap::getNumChannels() { return n; }
  54. }