Guest User

VideoTutorialsRock OpenGL ImageUploader.cpp

a guest
Nov 13th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.17 KB | None | 0 0
  1. /* Permission is hereby granted, free of charge, to any person obtaining a copy
  2.  * of this software and associated documentation files (the "Software"), to deal
  3.  * in the Software without restriction, including without limitation the rights
  4.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  5.  * copies of the Software, and to permit persons to whom the Software is
  6.  * furnished to do so, subject to the following conditions:
  7.  *
  8.  * The above notice and this permission notice shall be included in all copies
  9.  * or substantial portions of the Software.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17.  * SOFTWARE.
  18.  */
  19. /* File for "Terrain" lesson of the OpenGL tutorial on
  20.  * www.videotutorialsrock.com
  21.  */
  22.  
  23.  
  24.  
  25. #include <assert.h>
  26. #include <fstream>
  27.  
  28. #include "imageloader.h"
  29.  
  30. using namespace std;
  31.  
  32. Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h) {
  33.    
  34. }
  35.  
  36. Image::~Image() {
  37.     delete[] pixels;
  38. }
  39.  
  40. namespace {
  41.     //Converts a four-character array to an integer, using little-endian form
  42.     int toInt(const char* bytes) {
  43.         return (int)(((unsigned char)bytes[3] << 24) |
  44.                      ((unsigned char)bytes[2] << 16) |
  45.                      ((unsigned char)bytes[1] << 8) |
  46.                      (unsigned char)bytes[0]);
  47.     }
  48.    
  49.     //Converts a two-character array to a short, using little-endian form
  50.     short toShort(const char* bytes) {
  51.         return (short)(((unsigned char)bytes[1] << 8) |
  52.                        (unsigned char)bytes[0]);
  53.     }
  54.    
  55.     //Reads the next four bytes as an integer, using little-endian form
  56.     int readInt(ifstream &input) {
  57.         char buffer[4];
  58.         input.read(buffer, 4);
  59.         return toInt(buffer);
  60.     }
  61.    
  62.     //Reads the next two bytes as a short, using little-endian form
  63.     short readShort(ifstream &input) {
  64.         char buffer[2];
  65.         input.read(buffer, 2);
  66.         return toShort(buffer);
  67.     }
  68.    
  69.     //Just like auto_ptr, but for arrays
  70.     template<class T>
  71.     class auto_array {
  72.         private:
  73.             T* array;
  74.             mutable bool isReleased;
  75.         public:
  76.             explicit auto_array(T* array_ = NULL) :
  77.                 array(array_), isReleased(false) {
  78.             }
  79.            
  80.             auto_array(const auto_array<T> &aarray) {
  81.                 array = aarray.array;
  82.                 isReleased = aarray.isReleased;
  83.                 aarray.isReleased = true;
  84.             }
  85.            
  86.             ~auto_array() {
  87.                 if (!isReleased && array != NULL) {
  88.                     delete[] array;
  89.                 }
  90.             }
  91.            
  92.             T* get() const {
  93.                 return array;
  94.             }
  95.            
  96.             T &operator*() const {
  97.                 return *array;
  98.             }
  99.            
  100.             void operator=(const auto_array<T> &aarray) {
  101.                 if (!isReleased && array != NULL) {
  102.                     delete[] array;
  103.                 }
  104.                 array = aarray.array;
  105.                 isReleased = aarray.isReleased;
  106.                 aarray.isReleased = true;
  107.             }
  108.            
  109.             T* operator->() const {
  110.                 return array;
  111.             }
  112.            
  113.             T* release() {
  114.                 isReleased = true;
  115.                 return array;
  116.             }
  117.            
  118.             void reset(T* array_ = NULL) {
  119.                 if (!isReleased && array != NULL) {
  120.                     delete[] array;
  121.                 }
  122.                 array = array_;
  123.             }
  124.            
  125.             T* operator+(int i) {
  126.                 return array + i;
  127.             }
  128.            
  129.             T &operator[](int i) {
  130.                 return array[i];
  131.             }
  132.     };
  133. }
  134.  
  135. Image* loadBMP(const char* filename) {
  136.     ifstream input;
  137.     input.open(filename, ifstream::binary);
  138.     assert(!input.fail() || !"Could not find file");
  139.     char buffer[2];
  140.     input.read(buffer, 2);
  141.     assert(buffer[0] == 'B' && buffer[1] == 'M' || !"Not a bitmap file");
  142.     input.ignore(8);
  143.     int dataOffset = readInt(input);
  144.    
  145.     //Read the header
  146.     int headerSize = readInt(input);
  147.     int width;
  148.     int height;
  149.     switch(headerSize) {
  150.         case 40:
  151.             //V3
  152.             width = readInt(input);
  153.             height = readInt(input);
  154.             input.ignore(2);
  155.             assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
  156.             assert(readShort(input) == 0 || !"Image is compressed");
  157.             break;
  158.         case 12:
  159.             //OS/2 V1
  160.             width = readShort(input);
  161.             height = readShort(input);
  162.             input.ignore(2);
  163.             assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
  164.             break;
  165.         case 64:
  166.             //OS/2 V2
  167.             assert(!"Can't load OS/2 V2 bitmaps");
  168.             break;
  169.         case 108:
  170.             //Windows V4
  171.             assert(!"Can't load Windows V4 bitmaps");
  172.             break;
  173.         case 124:
  174.             //Windows V5
  175.             assert(!"Can't load Windows V5 bitmaps");
  176.             break;
  177.         default:
  178.             assert(!"Unknown bitmap format");
  179.     }
  180.    
  181.     //Read the data
  182.     int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
  183.     int size = bytesPerRow * height;
  184.     auto_array<char> pixels(new char[size]);
  185.     input.seekg(dataOffset, ios_base::beg);
  186.     input.read(pixels.get(), size);
  187.    
  188.     //Get the data into the right format
  189.     auto_array<char> pixels2(new char[width * height * 3]);
  190.     for(int y = 0; y < height; y++) {
  191.         for(int x = 0; x < width; x++) {
  192.             for(int c = 0; c < 3; c++) {
  193.                 pixels2[3 * (width * y + x) + c] =
  194.                     pixels[bytesPerRow * y + 3 * x + (2 - c)];
  195.             }
  196.         }
  197.     }
  198.    
  199.     input.close();
  200.     return new Image(pixels2.release(), width, height);
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
Advertisement
Add Comment
Please, Sign In to add comment