Guest User

ByteMap.h

a guest
Dec 7th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <algorithm>
  4. #include <math.h>
  5.  
  6. #include <CMisc.h>
  7.  
  8. #include "Mipmapable.h"
  9.  
  10. class ByteMap : public Mipmapable<ByteMap>
  11. {
  12. public:
  13.     ByteMap::ByteMap(int width, int height) {
  14.         if (width < 1 || height < 1)
  15.             THROW("Error: Bad dimensions (" << width << "x" << height << ") on ByteMap; must be at least 1x1.");
  16.         w = width;
  17.         h = height;
  18.         data = (BYTE*) malloc(sizeof(BYTE) * w * h);
  19.     }
  20.  
  21.     ByteMap::~ByteMap(void) {
  22.         free(data);
  23.     }
  24.  
  25.     void setByte(int x, int y, BYTE b) {
  26.         data[x + y * h] = b;
  27.     }
  28.  
  29.     BYTE getByte(int x, int y) {
  30.         return data[x + y * h];
  31.     }
  32.  
  33.     int getWidth() {
  34.         return w;
  35.     }
  36.  
  37.     int getHeight() {
  38.         return h;
  39.     }
  40.  
  41.     /*bool canShrink() {
  42.         return (w / 2 * 2 == w) && (h / 2 * 2 == h);
  43.     }*/
  44.  
  45.     ByteMap* makeMipMap(void) {
  46.         int width = w / 2;
  47.         int height = h / 2;
  48.         ByteMap* mipMap = new ByteMap(width, height);
  49.         for (int x = 0; x < width; x++) {
  50.             for (int y = 0; y < height; y++) {
  51.                 BYTE interpolatedColorCorrected
  52.                     = (BYTE)((int)getByte(2 * x, 2 * y)
  53.                     + (int)getByte(2 * x + 1, 2 * y)
  54.                     + (int)getByte(2 * x, 2 * y + 1)
  55.                     + (int)getByte(2 * x + 1, 2 * y + 1) / 4);
  56.                 mipMap->setByte(x, y, interpolatedColorCorrected);
  57.             }
  58.         }
  59.         return mipMap;
  60.     }
  61.  
  62. private:
  63.     int w;
  64.     int h;
  65.     BYTE* data;
  66. };
Advertisement
Add Comment
Please, Sign In to add comment