Repository navigation
Expand file tree
/
Copy pathImageDecoder.m
More file actions
68 lines (53 loc) · 1.99 KB
/
Copy pathImageDecoder.m
File metadata and controls
68 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import "DSPManager.h"
@interface ImageDecoder : NSObject
- (NSImage*)decodeJPEGWithDSP:(NSData*)jpegData;
@end
// ImageDecoder.m
@implementation ImageDecoder
- (NSImage*)decodeJPEGWithDSP:(NSData*)jpegData {
if ([[DSPManager sharedManager] dspAvailable]) {
// Offload DCT to DSP
[[DSPManager sharedManager] sendCommand:DSP_CMD_JPEG_DCT
data:jpegData];
// Process while waiting for DSP
NSData* dctData = [[DSPManager sharedManager]
readResultBuffer:jpegData.length];
return [self _processDCTResults:dctData];
}
else {
// Fallback to software
return [self decodeJPEGSoftware:jpegData];
}
}
- (NSImage*)_processDCTResults:(NSData*)dctData {
// Convert DSP56000 fixed-point output to floats
int24_t* dctCoefficients = (int24_t*)dctData.bytes;
float* floatCoefficients = malloc(dctData.length * sizeof(float));
for (int i = 0; i < dctData.length/sizeof(int24_t); i++) {
// Use DSP's 24-bit fixed-point format
floatCoefficients[i] = dctCoefficients[i] / (float)(1 << 23);
}
// Continue with IDCT and color space conversion
return [self _convertColorSpace:floatCoefficients];
}
@end
- (NSImage*)decodeImageData:(NSData*)data {
DSP_TRY_CATCH(JPEGAcceleration, decode, data)
}
// DSP-accelerated path
- (NSImage*)decodeWithDSP:(NSData*)data {
if(![self validateJPEGHeader:data]) {
[NSException raise:@"InvalidJPEG" format:@"Bad JPEG structure"];
}
// DSP-specific processing
[[DSPManager sharedManager] sendCommand:DSP_CMD_JPEG_DCT data:data];
NSData* processed = [[DSPManager sharedManager] readResultBuffer:data.length];
return [self convertDCTToImage:processed];
}
// CPU fallback
- (NSImage*)decodeWithoutDSP:(NSData*)data {
JPEGDecoder* softDecoder = [JPEGDecoder new];
NSImage* result = [softDecoder softwareDecode:data];
[softDecoder release];
return result;
}