Etc.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "EtcConfig.h"
  17. #include "Etc.h"
  18. #include "EtcFilter.h"
  19. #include <string.h>
  20. namespace Etc
  21. {
  22. // ----------------------------------------------------------------------------------------------------
  23. // C-style inteface to the encoder
  24. //
  25. void Encode(float *a_pafSourceRGBA,
  26. unsigned int a_uiSourceWidth,
  27. unsigned int a_uiSourceHeight,
  28. Image::Format a_format,
  29. ErrorMetric a_eErrMetric,
  30. float a_fEffort,
  31. unsigned int a_uiJobs,
  32. unsigned int a_uiMaxJobs,
  33. unsigned char **a_ppaucEncodingBits,
  34. unsigned int *a_puiEncodingBitsBytes,
  35. unsigned int *a_puiExtendedWidth,
  36. unsigned int *a_puiExtendedHeight,
  37. int *a_piEncodingTime_ms, bool a_bVerboseOutput)
  38. {
  39. Image image(a_pafSourceRGBA, a_uiSourceWidth,
  40. a_uiSourceHeight,
  41. a_eErrMetric);
  42. image.m_bVerboseOutput = a_bVerboseOutput;
  43. image.Encode(a_format, a_eErrMetric, a_fEffort, a_uiJobs, a_uiMaxJobs);
  44. *a_ppaucEncodingBits = image.GetEncodingBits();
  45. *a_puiEncodingBitsBytes = image.GetEncodingBitsBytes();
  46. *a_puiExtendedWidth = image.GetExtendedWidth();
  47. *a_puiExtendedHeight = image.GetExtendedHeight();
  48. *a_piEncodingTime_ms = image.GetEncodingTimeMs();
  49. }
  50. void EncodeMipmaps(float *a_pafSourceRGBA,
  51. unsigned int a_uiSourceWidth,
  52. unsigned int a_uiSourceHeight,
  53. Image::Format a_format,
  54. ErrorMetric a_eErrMetric,
  55. float a_fEffort,
  56. unsigned int a_uiJobs,
  57. unsigned int a_uiMaxJobs,
  58. unsigned int a_uiMaxMipmaps,
  59. unsigned int a_uiMipFilterFlags,
  60. RawImage* a_pMipmapImages,
  61. int *a_piEncodingTime_ms,
  62. bool a_bVerboseOutput)
  63. {
  64. auto mipWidth = a_uiSourceWidth;
  65. auto mipHeight = a_uiSourceHeight;
  66. int totalEncodingTime = 0;
  67. for(unsigned int mip = 0; mip < a_uiMaxMipmaps && mipWidth >= 1 && mipHeight >= 1; mip++)
  68. {
  69. float* pImageData = nullptr;
  70. float* pMipImage = nullptr;
  71. if(mip == 0)
  72. {
  73. pImageData = a_pafSourceRGBA;
  74. }
  75. else
  76. {
  77. pMipImage = new float[mipWidth*mipHeight*4];
  78. if(FilterTwoPass(a_pafSourceRGBA, a_uiSourceWidth, a_uiSourceHeight, pMipImage, mipWidth, mipHeight, a_uiMipFilterFlags, Etc::FilterLanczos3) )
  79. {
  80. pImageData = pMipImage;
  81. }
  82. }
  83. if ( pImageData )
  84. {
  85. Image image(pImageData, mipWidth, mipHeight, a_eErrMetric);
  86. image.m_bVerboseOutput = a_bVerboseOutput;
  87. image.Encode(a_format, a_eErrMetric, a_fEffort, a_uiJobs, a_uiMaxJobs);
  88. a_pMipmapImages[mip].paucEncodingBits = std::shared_ptr<unsigned char>(image.GetEncodingBits(), [](unsigned char *p) { delete[] p; });
  89. a_pMipmapImages[mip].uiEncodingBitsBytes = image.GetEncodingBitsBytes();
  90. a_pMipmapImages[mip].uiExtendedWidth = image.GetExtendedWidth();
  91. a_pMipmapImages[mip].uiExtendedHeight = image.GetExtendedHeight();
  92. totalEncodingTime += image.GetEncodingTimeMs();
  93. }
  94. if(pMipImage)
  95. {
  96. delete[] pMipImage;
  97. }
  98. if (!pImageData)
  99. {
  100. break;
  101. }
  102. mipWidth >>= 1;
  103. mipHeight >>= 1;
  104. }
  105. *a_piEncodingTime_ms = totalEncodingTime;
  106. }
  107. // ----------------------------------------------------------------------------------------------------
  108. //
  109. }