forked from jlamarche/Tile-Cutter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSBitmapImageRep-Tile.m
137 lines (114 loc) · 4.16 KB
/
NSBitmapImageRep-Tile.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
//
// NSBitmapImageRep-Tile.m
// Tile Cutter
//
// Created by jeff on 10/30/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "NSBitmapImageRep-Tile.h"
@implementation NSBitmapImageRep(Tile)
-(NSImage *)subImageWithTileWidth:(CGFloat)tileWidth
tileHeight:(CGFloat)tileHeight
column:(NSUInteger)column
row:(NSUInteger)row
{
return [self subImageWithTileWidth:tileWidth tileHeight:tileHeight column: column row:row rigidSize: NO POTSize: NO];
}
-(NSImage *)subImageWithTileWidth:(CGFloat)tileWidth
tileHeight:(CGFloat)tileHeight
column:(NSUInteger)column
row:(NSUInteger)row
rigidSize:(BOOL) rigid
{
return [self subImageWithTileWidth:tileWidth tileHeight:tileHeight column: column row:row rigidSize: NO POTSize: NO];
}
-(NSImage *)subImageWithTileWidth:(CGFloat)tileWidth
tileHeight:(CGFloat)tileHeight
column:(NSUInteger)column
row:(NSUInteger)row
rigidSize:(BOOL) rigid
POTSize:(BOOL) potSize
{
int width = [self pixelsWide];
int height = [self pixelsHigh];
int bytesPerPixel = [self bitsPerPixel] / 8;
int theRow = row * tileHeight;
int theCol = column * tileWidth;
int i,x,y;
unsigned char *p1, *p2;
int lastCol;
int outputWidth;
if ( !rigid && (theCol + tileWidth > width) ) // last column, not full size
{
lastCol = width;
outputWidth = (width - theCol);
if (potSize)
outputWidth = (int)pow( 2, ceil(log2((double)outputWidth)));
}
else
{
lastCol = theCol + tileWidth;
outputWidth = tileWidth;
}
int lastRow, outputHeight;
if ( !rigid && (theRow + tileHeight > height) )
{
lastRow = height;
outputHeight = (height - theRow);
if (potSize)
outputHeight = (int)pow( 2, ceil(log2((double)outputHeight)));
}
else
{
lastRow = theRow + tileHeight;
outputHeight = tileHeight;
}
NSImage *ret = [[NSImage alloc] initWithSize:NSMakeSize(outputWidth,outputHeight)];
NSBitmapImageRep *retRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:outputWidth
pixelsHigh:outputHeight
bitsPerSample:[self bitsPerSample]
samplesPerPixel:[self samplesPerPixel]
hasAlpha:[self hasAlpha]
isPlanar:[self isPlanar]
colorSpaceName:[self colorSpaceName]
bitmapFormat:[self bitmapFormat]
bytesPerRow:[self bytesPerRow]
bitsPerPixel:[self bitsPerPixel]];
unsigned char *srcData = [self bitmapData];
unsigned char *destData = [retRep bitmapData];
for (x = theCol; x < lastCol; x++)
{
for (y = theRow; y < lastRow; y++)
{
p1 = srcData + bytesPerPixel * (y * width + x);
p2 = destData + bytesPerPixel * ((y - theRow) * width + (x - theCol));
if ( x >= width || y >= height)
{
// fill with zeroes pixels outside self.size
for (i = 0; i < bytesPerPixel; i++)
p2[i] = 0;
}
else
{
// copy pixels as usual
for (i = 0; i < bytesPerPixel; i++)
p2[i] = p1[i];
}
}
}
[ret addRepresentation:retRep];
[retRep release];
return [ret autorelease];
}
-(NSUInteger)columnsWithTileWidth:(CGFloat)tileWidth
{
CGFloat columns = [self size].width / tileWidth;
return (NSUInteger) ceilf(columns);
}
-(NSUInteger)rowsWithTileHeight:(CGFloat)tileHeight
{
CGFloat rows = [self size].height / tileHeight;
return (NSUInteger) ceilf(rows);
}
@end