zoh.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // the zoh compressor and decompressor
  10. #include "tile.h"
  11. #include "zoh.h"
  12. #include <string.h> // memcpy
  13. using namespace ZOH;
  14. bool ZOH::isone(const char *block)
  15. {
  16. char code = block[0] & 0x1F;
  17. return (code == 0x03 || code == 0x07 || code == 0x0b || code == 0x0f);
  18. }
  19. void ZOH::compress(const Tile &t, char *block)
  20. {
  21. char oneblock[ZOH::BLOCKSIZE], twoblock[ZOH::BLOCKSIZE];
  22. float mseone = ZOH::compressone(t, oneblock);
  23. float msetwo = ZOH::compresstwo(t, twoblock);
  24. if (mseone <= msetwo)
  25. memcpy(block, oneblock, ZOH::BLOCKSIZE);
  26. else
  27. memcpy(block, twoblock, ZOH::BLOCKSIZE);
  28. }
  29. void ZOH::decompress(const char *block, Tile &t)
  30. {
  31. if (ZOH::isone(block))
  32. ZOH::decompressone(block, t);
  33. else
  34. ZOH::decompresstwo(block, t);
  35. }
  36. /*
  37. void ZOH::compress(string inf, string zohf)
  38. {
  39. Array2D<Rgba> pixels;
  40. int w, h;
  41. char block[ZOH::BLOCKSIZE];
  42. Exr::readRgba(inf, pixels, w, h);
  43. FILE *zohfile = fopen(zohf.c_str(), "wb");
  44. if (zohfile == NULL) throw "Unable to open .zoh file for write";
  45. // stuff for progress bar O.o
  46. int ntiles = ((h+Tile::TILE_H-1)/Tile::TILE_H)*((w+Tile::TILE_W-1)/Tile::TILE_W);
  47. int tilecnt = 0;
  48. int ndots = 25;
  49. int dotcnt = 0;
  50. printf("Progress [");
  51. for (int i=0; i<ndots;++i) printf(" ");
  52. printf("]\rProgress ["); fflush(stdout);
  53. // convert to tiles and compress each tile
  54. for (int y=0; y<h; y+=Tile::TILE_H)
  55. {
  56. int ysize = min(Tile::TILE_H, h-y);
  57. for (int x=0; x<w; x+=Tile::TILE_W)
  58. {
  59. int xsize = min(Tile::TILE_W, w-x);
  60. Tile t(xsize, ysize);
  61. t.insert(pixels, x, y);
  62. ZOH::compress(t, block);
  63. if (fwrite(block, sizeof(char), ZOH::BLOCKSIZE, zohfile) != ZOH::BLOCKSIZE)
  64. throw "File error on write";
  65. // progress bar
  66. ++tilecnt;
  67. if (tilecnt > (ntiles * dotcnt)/ndots) { printf("."); fflush(stdout); ++dotcnt; }
  68. }
  69. }
  70. printf("]\n"); // advance to next line finally
  71. if (fclose(zohfile)) throw "Close failed on .zoh file";
  72. }
  73. static int str2int(std::string s)
  74. {
  75. int thing;
  76. std::stringstream str (stringstream::in | stringstream::out);
  77. str << s;
  78. str >> thing;
  79. return thing;
  80. }
  81. // zoh file name is ...-w-h.zoh, extract width and height
  82. static void extract(string zohf, int &w, int &h)
  83. {
  84. size_t n = zohf.rfind('.', zohf.length()-1);
  85. size_t n1 = zohf.rfind('-', n-1);
  86. size_t n2 = zohf.rfind('-', n1-1);
  87. string width = zohf.substr(n2+1, n1-n2-1);
  88. w = str2int(width);
  89. string height = zohf.substr(n1+1, n-n1-1);
  90. h = str2int(height);
  91. }
  92. static int mode_to_prec[] = {
  93. 10,7,11,10,
  94. 10,7,11,11,
  95. 10,7,11,12,
  96. 10,7,9,16,
  97. 10,7,8,-1,
  98. 10,7,8,-1,
  99. 10,7,8,-1,
  100. 10,7,6,-1,
  101. };
  102. static int shapeindexhist[32], modehist[32], prechistone[16], prechisttwo[16], oneregion, tworegions;
  103. static void stats(char block[ZOH::BLOCKSIZE])
  104. {
  105. char mode = block[0] & 0x1F; if ((mode & 0x3) == 0) mode = 0; if ((mode & 0x3) == 1) mode = 1; modehist[mode]++;
  106. int prec = mode_to_prec[mode];
  107. nvAssert (prec != -1);
  108. if (!ZOH::isone(block))
  109. {
  110. tworegions++;
  111. prechisttwo[prec]++;
  112. int shapeindex = ((block[0] & 0xe0) >> 5) | ((block[1] & 0x3) << 3);
  113. shapeindexhist[shapeindex]++;
  114. }
  115. else
  116. {
  117. oneregion++;
  118. prechistone[prec]++;
  119. }
  120. }
  121. static void printstats()
  122. {
  123. printf("\nPrecision histogram 10b to 16b one region: "); for (int i=10; i<=16; ++i) printf("%d,", prechistone[i]);
  124. printf("\nPrecision histogram 6b to 11b two regions: "); for (int i=6; i<=11; ++i) printf("%d,", prechisttwo[i]);
  125. printf("\nMode histogram: "); for (int i=0; i<32; ++i) printf("%d,", modehist[i]);
  126. printf("\nShape index histogram: "); for (int i=0; i<32; ++i) printf("%d,", shapeindexhist[i]);
  127. printf("\nOne region %5.2f%% Two regions %5.2f%%", 100.0*oneregion/float(oneregion+tworegions), 100.0*tworegions/float(oneregion+tworegions));
  128. printf("\n");
  129. }
  130. void ZOH::decompress(string zohf, string outf)
  131. {
  132. Array2D<Rgba> pixels;
  133. int w, h;
  134. char block[ZOH::BLOCKSIZE];
  135. extract(zohf, w, h);
  136. FILE *zohfile = fopen(zohf.c_str(), "rb");
  137. if (zohfile == NULL) throw "Unable to open .zoh file for read";
  138. pixels.resizeErase(h, w);
  139. // convert to tiles and decompress each tile
  140. for (int y=0; y<h; y+=Tile::TILE_H)
  141. {
  142. int ysize = min(Tile::TILE_H, h-y);
  143. for (int x=0; x<w; x+=Tile::TILE_W)
  144. {
  145. int xsize = min(Tile::TILE_W, w-x);
  146. Tile t(xsize, ysize);
  147. if (fread(block, sizeof(char), ZOH::BLOCKSIZE, zohfile) != ZOH::BLOCKSIZE)
  148. throw "File error on read";
  149. stats(block); // collect statistics
  150. ZOH::decompress(block, t);
  151. t.extract(pixels, x, y);
  152. }
  153. }
  154. if (fclose(zohfile)) throw "Close failed on .zoh file";
  155. Exr::writeRgba(outf, pixels, w, h);
  156. #ifndef EXTERNAL_RELEASE
  157. printstats(); // print statistics
  158. #endif
  159. }
  160. */