forked from zepouet/Xee-xCode-4.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XeeExperimentalImage1Saver.m
163 lines (136 loc) · 3.25 KB
/
XeeExperimentalImage1Saver.m
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#import "XeeExperimentalImage1Saver.h"
#import "XeeBitmapImage.h"
#import <XADMaster/CSFileHandle.h>
static void f(int n,int *xp,int *yp);
static int fi(int x,int y);
@implementation XeeExperimentalImage1Saver
+(BOOL)canSaveImage:(XeeImage *)img
{
return YES;
/* CGImageRef cgimage=[img createCGImage];
if(!img) return NO;
int depth=CGImageGetBitsPerComponent(cgimage);
int info=CGImageGetBitmapInfo(cgimage);
CGImageRelease(cgimage);
return depth==8;*/
}
-(id)initWithImage:(XeeImage *)img
{
if(self=[super initWithImage:img])
{
}
return self;
}
-(NSString *)format { return @"Xee Experimental Image Format 1"; }
-(NSString *)extension { return @"xei1"; }
-(BOOL)save:(NSString *)filename
{
BOOL res=NO;
CGImageRef cgimage=[image createCGImage];
if(cgimage)
{
int pixelwidth=CGImageGetWidth(cgimage);
int pixelheight=CGImageGetHeight(cgimage);
XeeBitmapImage *bmimage=[[[XeeBitmapImage alloc] initWithType:XeeBitmapTypeNRGB8 width:pixelwidth height:pixelheight] autorelease];
if(image)
{
CGContextRef cgcontext=[bmimage createCGContext];
if(cgcontext)
{
CGContextDrawImage(cgcontext,CGRectMake(0,0,pixelwidth,pixelheight),cgimage);
CGContextRelease(cgcontext);
CSFileHandle *fh=[CSFileHandle fileHandleForWritingAtPath:filename];
if(fh)
{
[fh writeInt32BE:'XEI1'];
[fh writeInt32BE:pixelwidth];
[fh writeInt32BE:pixelheight];
uint8_t *data=[bmimage data];
int bpr=[bmimage bytesPerRow];
int left,n,x,y;
uint8_t prev;
left=pixelwidth*pixelheight;
n=0;
prev=0;
while(left)
{
f(n++,&x,&y);
if(x<pixelwidth&y<pixelheight)
{
uint32_t *row=(uint32_t *)(data+y*bpr);
uint8_t val=XeeGetRFromNRGB8(row[x]);
[fh writeUInt8:(uint8_t)(val-prev)];
prev=val;
left--;
}
}
left=pixelwidth*pixelheight;
n=0;
prev=0;
while(left)
{
f(n++,&x,&y);
if(x<pixelwidth&y<pixelheight)
{
uint32_t *row=(uint32_t *)(data+y*bpr);
uint8_t val=XeeGetGFromNRGB8(row[x]);
[fh writeUInt8:(uint8_t)(val-prev)];
prev=val;
left--;
}
}
left=pixelwidth*pixelheight;
n=0;
prev=0;
while(left)
{
f(n++,&x,&y);
if(x<pixelwidth&y<pixelheight)
{
uint32_t *row=(uint32_t *)(data+y*bpr);
uint8_t val=XeeGetBFromNRGB8(row[x]);
[fh writeUInt8:(uint8_t)(val-prev)];
prev=val;
left--;
}
}
res=YES;
}
}
}
CGImageRelease(cgimage);
}
return res;
}
@end
static int transform_table[4][4]={{0,1,2,3},{0,2,1,3},{3,2,1,0},{3,1,2,0}};
static int locations[4]={0,1,3,2};
static void f(int n,int *xp,int *yp)
{
static int transforms[4]={1,0,0,3};
int x=0,y=0;
int trans=0;
for(int i=30;i>=0;i-=2)
{
int m=(n>>i)&3;
int bits=transform_table[trans][locations[m]];
x=(x<<1)|((bits>>1)&1);
y=(y<<1)|(bits&1);
trans^=transforms[m];
}
*xp=x; *yp=y;
}
static int fi(int x,int y)
{
static int transforms[4]={1,0,3,0};
int n=0;
int trans=0;
for(int i=15;i>=0;i--)
{
int m=transform_table[trans][((y>>i)&1)|(((x>>i)&1)<<1)];
int bits=locations[m];
n=(n<<2)|bits;
trans^=transforms[m];
}
return n;
}