Skip to content
This repository was archived by the owner on Oct 31, 2022. It is now read-only.

Commit 6aae821

Browse files
committed
Minor code tidy: move channel permutation into ImageMath.c
1 parent ff29763 commit 6aae821

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

source/HapCompressor.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "Tasks.h"
4242
#include "Buffers.h"
4343
#include "DXTEncoder.h"
44-
#include <Accelerate/Accelerate.h>
44+
#include "ImageMath.h"
4545

4646
/*
4747
Defines determining the method of DXT compression.
@@ -681,28 +681,14 @@ static void Background_Encode(void *info)
681681
case k32RGBAPixelFormat:
682682
if (sourcePixelFormat == k32BGRAPixelFormat)
683683
{
684-
vImage_Buffer src = {
685-
sourceBaseAddress,
686-
glob->height,
687-
glob->width,
688-
sourceBytesPerRow
689-
};
690-
691-
vImage_Buffer dst = {
692-
HapCodecBufferGetBaseAddress(formatConvertBuffer),
693-
glob->height,
694-
glob->width,
695-
glob->formatConvertBufferBytesPerRow
696-
};
697-
698684
uint8_t permuteMap[] = {2, 1, 0, 3};
699-
vImage_Error permuteError = vImagePermuteChannels_ARGB8888(&src, &dst, permuteMap, kvImageNoFlags);
700-
701-
if (permuteError != kvImageNoError)
702-
{
703-
err = internalComponentErr;
704-
goto bail;
705-
}
685+
ImageMath_Permute8888(sourceBaseAddress,
686+
sourceBytesPerRow,
687+
HapCodecBufferGetBaseAddress(formatConvertBuffer),
688+
glob->formatConvertBufferBytesPerRow,
689+
glob->width,
690+
glob->height,
691+
permuteMap);
706692
}
707693
else
708694
{

source/HapDecompressor.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
#ifdef HAP_SQUISH_DECODE
6666
#include "squish-c.h"
67-
#include <Accelerate/Accelerate.h>
67+
#include "ImageMath.h"
6868
#endif
6969

7070
// Data structures
@@ -671,17 +671,14 @@ pascal ComponentResult Hap_DDrawBand(HapDecompressorGlobals glob, ImageSubCodecD
671671
// TODO: avoid the permute stage
672672
if (myDrp->needsPermute)
673673
{
674-
vImage_Buffer srcV, dstV;
675-
srcV.data = HapCodecBufferGetBaseAddress(myDrp->permuteBuffer);
676-
srcV.width = myDrp->dxtWidth;
677-
srcV.height = myDrp->dxtHeight;
678-
srcV.rowBytes = myDrp->dxtWidth * 4;
679-
dstV.data = drp->baseAddr;
680-
dstV.width = myDrp->dxtWidth;
681-
dstV.height = myDrp->dxtHeight;
682-
dstV.rowBytes = drp->rowBytes;
683674
uint8_t permuteMap[] = {2, 1, 0, 3};
684-
vImagePermuteChannels_ARGB8888(&srcV, &dstV, permuteMap, kvImageNoFlags);
675+
ImageMath_Permute8888(HapCodecBufferGetBaseAddress(myDrp->permuteBuffer),
676+
myDrp->dxtWidth * 4,
677+
drp->baseAddr,
678+
drp->rowBytes,
679+
myDrp->dxtWidth,
680+
myDrp->dxtHeight,
681+
permuteMap);
685682
}
686683
else
687684
{

source/ImageMath.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,56 @@ void ImageMath_MatrixMultiply8888(const void *src,
141141
}
142142
#endif // IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
143143
}
144+
145+
void ImageMath_Permute8888(const void *src,
146+
size_t src_bytes_per_row,
147+
void *dst,
148+
size_t dst_bytes_per_row,
149+
unsigned long width,
150+
unsigned long height,
151+
const uint8_t permuteMap[4])
152+
{
153+
#ifdef IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
154+
if (vImagePermuteChannels_ARGB8888 != NULL)
155+
{
156+
#endif // IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
157+
#ifdef IMAGE_MATH_USE_V_IMAGE
158+
vImage_Buffer v_src = {
159+
(void *)src,
160+
height,
161+
width,
162+
src_bytes_per_row
163+
};
164+
165+
vImage_Buffer v_dst = {
166+
dst,
167+
height,
168+
width,
169+
dst_bytes_per_row
170+
};
171+
172+
vImagePermuteChannels_ARGB8888(&v_src, &v_dst, permuteMap, kvImageNoFlags);
173+
#endif // IMAGE_MATH_USE_V_IMAGE
174+
#ifdef IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
175+
}
176+
else
177+
{
178+
#endif // IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
179+
#if !defined(IMAGE_MATH_USE_V_IMAGE) || defined(IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED)
180+
for (unsigned long y = 0; y < height; y++) {
181+
for (unsigned long x = 0; x < width; x++) {
182+
const uint8_t *pixel_src = src + (x * 4);
183+
uint8_t *pixel_dst = dst + (x * 4);
184+
185+
for( int i = 0; i < 4; i++ ) {
186+
pixel_dst[i] = pixel_src[permuteMap[i]];
187+
}
188+
}
189+
src += src_bytes_per_row;
190+
dst += dst_bytes_per_row;
191+
}
192+
#endif // !defined(IMAGE_MATH_USE_V_IMAGE) || defined(IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED)
193+
#ifdef IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
194+
}
195+
#endif // IMAGE_MATH_USE_V_IMAGE_WEAK_LINKED
196+
}

source/ImageMath.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ void ImageMath_MatrixMultiply8888(const void *src,
4444
const int16_t *pre_bias, // An array of 4 int16_t or NULL, added before matrix op
4545
const int32_t *post_bias); // An array of 4 int32_t or NULL, added after matrix op
4646

47+
void ImageMath_Permute8888(const void *src,
48+
size_t src_bytes_per_row,
49+
void *dst,
50+
size_t dst_bytes_per_row,
51+
unsigned long width,
52+
unsigned long height,
53+
const uint8_t permuteMap[4]); // positions are dst channel order, values are src channel order
54+
4755
#endif

0 commit comments

Comments
 (0)