tile.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2007 nVidia, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  5. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
  6. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7. See the License for the specific language governing permissions and limitations under the License.
  8. */
  9. #ifndef _ZOH_TILE_H
  10. #define _ZOH_TILE_H
  11. #include "zoh_utils.h"
  12. #include "nvmath/vector.h"
  13. #include <math.h>
  14. namespace ZOH {
  15. //#define USE_IMPORTANCE_MAP 1 // define this if you want to increase importance of some pixels in tile
  16. class Tile
  17. {
  18. public:
  19. // NOTE: this returns the appropriately-clamped BIT PATTERN of the half as an INTEGRAL float value
  20. static float half2float(uint16 h)
  21. {
  22. return (float) Utils::ushort_to_format(h);
  23. }
  24. // NOTE: this is the inverse of the above operation
  25. static uint16 float2half(float f)
  26. {
  27. return Utils::format_to_ushort((int)f);
  28. }
  29. // look for adjacent pixels that are identical. if there are enough of them, increase their importance
  30. void generate_importance_map()
  31. {
  32. // initialize
  33. for (int y=0; y<size_y; ++y)
  34. for (int x=0; x<size_x; ++x)
  35. {
  36. // my importance is increased if I am identical to any of my 4-neighbors
  37. importance_map[y][x] = match_4_neighbor(x,y) ? 5.0f : 1.0f;
  38. }
  39. }
  40. bool is_equal(int x, int y, int xn, int yn)
  41. {
  42. if (xn < 0 || xn >= size_x || yn < 0 || yn >= size_y)
  43. return false;
  44. return( (data[y][x].x == data[yn][xn].x) &&
  45. (data[y][x].y == data[yn][xn].y) &&
  46. (data[y][x].z == data[yn][xn].z) );
  47. }
  48. #ifdef USE_IMPORTANCE_MAP
  49. bool match_4_neighbor(int x, int y)
  50. {
  51. return is_equal(x,y,x-1,y) || is_equal(x,y,x+1,y) || is_equal(x,y,x,y-1) || is_equal(x,y,x,y+1);
  52. }
  53. #else
  54. bool match_4_neighbor(int, int)
  55. {
  56. return false;
  57. }
  58. #endif
  59. Tile() {};
  60. ~Tile(){};
  61. Tile(int xs, int ys) {size_x = xs; size_y = ys;}
  62. static const int TILE_H = 4;
  63. static const int TILE_W = 4;
  64. static const int TILE_TOTAL = TILE_H * TILE_W;
  65. nv::Vector3 data[TILE_H][TILE_W];
  66. float importance_map[TILE_H][TILE_W];
  67. int size_x, size_y; // actual size of tile
  68. };
  69. }
  70. #endif // _ZOH_TILE_H