Debian Codegraph @

third_party/etc2comp/EtcLib/Etc/Etc.cpp

raw
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
17 #include "EtcConfig.h"
18 #include "Etc.h"
19 #include "EtcFilter.h"
20
21 #include <string.h>
22
23 namespace Etc
24 {
25 // ----------------------------------------------------------------------------------------------------
26 // C-style inteface to the encoder
27 //
28 void Encode(float *a_pafSourceRGBA,
29 unsigned int a_uiSourceWidth,
30 unsigned int a_uiSourceHeight,
31 Image::Format a_format,
32 ErrorMetric a_eErrMetric,
33 float a_fEffort,
34 unsigned int a_uiJobs,
35 unsigned int a_uiMaxJobs,
36 unsigned char **a_ppaucEncodingBits,
37 unsigned int *a_puiEncodingBitsBytes,
38 unsigned int *a_puiExtendedWidth,
39 unsigned int *a_puiExtendedHeight,
40 int *a_piEncodingTime_ms, bool a_bVerboseOutput)
41 {
42
43 Image image(a_pafSourceRGBA, a_uiSourceWidth,
44 a_uiSourceHeight,
45 a_eErrMetric);
46 image.m_bVerboseOutput = a_bVerboseOutput;
47 image.Encode(a_format, a_eErrMetric, a_fEffort, a_uiJobs, a_uiMaxJobs);
48
49 *a_ppaucEncodingBits = image.GetEncodingBits();
50 *a_puiEncodingBitsBytes = image.GetEncodingBitsBytes();
51 *a_puiExtendedWidth = image.GetExtendedWidth();
52 *a_puiExtendedHeight = image.GetExtendedHeight();
53 *a_piEncodingTime_ms = image.GetEncodingTimeMs();
54 }
55
56 void EncodeMipmaps(float *a_pafSourceRGBA,
57 unsigned int a_uiSourceWidth,
58 unsigned int a_uiSourceHeight,
59 Image::Format a_format,
60 ErrorMetric a_eErrMetric,
61 float a_fEffort,
62 unsigned int a_uiJobs,
63 unsigned int a_uiMaxJobs,
64 unsigned int a_uiMaxMipmaps,
65 unsigned int a_uiMipFilterFlags,
66 RawImage* a_pMipmapImages,
67 int *a_piEncodingTime_ms,
68 bool a_bVerboseOutput)
69 {
70 auto mipWidth = a_uiSourceWidth;
71 auto mipHeight = a_uiSourceHeight;
72 int totalEncodingTime = 0;
73 for(unsigned int mip = 0; mip < a_uiMaxMipmaps && mipWidth >= 1 && mipHeight >= 1; mip++)
74 {
75 float* pImageData = nullptr;
76 float* pMipImage = nullptr;
77
78 if(mip == 0)
79 {
80 pImageData = a_pafSourceRGBA;
81 }
82 else
83 {
84 pMipImage = new float[mipWidth*mipHeight*4];
85 if(FilterTwoPass(a_pafSourceRGBA, a_uiSourceWidth, a_uiSourceHeight, pMipImage, mipWidth, mipHeight, a_uiMipFilterFlags, Etc::FilterLanczos3) )
86 {
87 pImageData = pMipImage;
88 }
89 }
90
91 if ( pImageData )
92 {
93
94 Image image(pImageData, mipWidth, mipHeight, a_eErrMetric);
95
96 image.m_bVerboseOutput = a_bVerboseOutput;
97 image.Encode(a_format, a_eErrMetric, a_fEffort, a_uiJobs, a_uiMaxJobs);
98
99 a_pMipmapImages[mip].paucEncodingBits = std::shared_ptr<unsigned char>(image.GetEncodingBits(), [](unsigned char *p) { delete[] p; });
100 a_pMipmapImages[mip].uiEncodingBitsBytes = image.GetEncodingBitsBytes();
101 a_pMipmapImages[mip].uiExtendedWidth = image.GetExtendedWidth();
102 a_pMipmapImages[mip].uiExtendedHeight = image.GetExtendedHeight();
103
104 totalEncodingTime += image.GetEncodingTimeMs();
105 }
106
107 if(pMipImage)
108 {
109 delete[] pMipImage;
110 }
111
112 if (!pImageData)
113 {
114 break;
115 }
116
117 mipWidth >>= 1;
118 mipHeight >>= 1;
119 }
120
121 *a_piEncodingTime_ms = totalEncodingTime;
122 }
123
124
125 // ----------------------------------------------------------------------------------------------------
126 //
127
128 }