From d2cead637baef4e6d2390939605c412491b3ee58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E5=8F=8C=E6=98=8E?= Date: Wed, 9 Oct 2024 08:41:05 +0000 Subject: [PATCH] update library/src/main/ets/downsampling/DownsampleUtils.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 田双明 --- .../main/ets/downsampling/DownsampleUtils.ets | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/library/src/main/ets/downsampling/DownsampleUtils.ets b/library/src/main/ets/downsampling/DownsampleUtils.ets index ef17f42..2a8af50 100644 --- a/library/src/main/ets/downsampling/DownsampleUtils.ets +++ b/library/src/main/ets/downsampling/DownsampleUtils.ets @@ -12,6 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { DownsampleStrategy } from './DownsampleStartegy'; + export enum SampleSizeRounding { /** * Prefer to round the sample size up so that the image is downsampled to smaller than the @@ -26,6 +28,7 @@ export enum SampleSizeRounding { //(质量优先) QUALITY } + export function highestOneBit(i: number): number { i |= (i >> 1); i |= (i >> 2); @@ -33,4 +36,22 @@ export function highestOneBit(i: number): number { i |= (i >> 8); i |= (i >> 16); return i - (i >>> 1); +} + + +export function getScale(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number, + downsampType: DownsampleStrategy +): number { + if (downsampType === DownsampleStrategy.FIT_CENTER_MEMORY) { + const widthPercentage = requestedWidth / sourceWidth + const heightPercentage = requestedHeight / sourceHeight + return Math.min(widthPercentage, heightPercentage) + } else { + const maxIntegerFactor = Math.max(sourceHeight / requestedHeight, sourceWidth / requestedWidth); + return maxIntegerFactor === 0 ? 1 : 1 / highestOneBit(maxIntegerFactor); + + } +} +export function round(value: number): number { + return Math.floor(value + 0.5); } \ No newline at end of file