[go: up one dir, main page]

Menu

[r664]: / tags / 0.3 / RDCBitmap.m  Maximize  Restore  History

Download this file

194 lines (162 with data), 5.4 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
 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// RDCBitmap.m
// Remote Desktop
//
// Created by Craig Dooley on 8/28/06.
// Copyright (c) 2006 Craig Dooley <xlnxminusx@gmail.com>
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/* Notes: This class is a bit unsafe if not used properly: after calling one of
bitmapWithData, glyphWithData, or cursorWithData, those methods must not
be called again, or data will be leaked. This should be eventually changed
to follow a more rigid factory and/or initWith structure.
*/
#import "RDCBitmap.h"
@implementation RDCBitmap
-(void)bitmapWithData:(const unsigned char *)d size:(NSSize)s view:(RDCView *)v {
int bitsPerPixel = [v bitsPerPixel];
int bytesPerPixel = bitsPerPixel / 8;
uint8 *np, *nc;
const uint8 *p, *end;
unsigned realLength, newLength;
if (bitsPerPixel == 24) {
data = [[NSData alloc] initWithBytes:d length:s.width * s.height];
} else {
realLength = (int)s.width * (int)s.height * bytesPerPixel;
newLength = (int)s.width * (int)s.height * 3;
p = d;
end = p + realLength;
nc = np = malloc(newLength);
while (p < end) {
if (bitsPerPixel != 8) {
[v rgbForRDCColor:*(uint16 *)p r:&nc[0] g:&nc[1] b:&nc[2]];
nc += 3;
p += bytesPerPixel;
} else {
[v rgbForRDCColor:*p r:&nc[0] g:&nc[1] b:&nc[2]];
nc += 3;
p += bytesPerPixel;
}
}
data = [[NSData alloc] initWithBytesNoCopy:(void *)np length:newLength];
}
planes[0] = (unsigned char *)[data bytes];
planes[1] = NULL;
bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:planes
pixelsWide:(int)s.width
pixelsHigh:(int)s.height
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:s.width * 3
bitsPerPixel:0];
image = [[NSImage alloc] init];
[image addRepresentation:bitmap];
[image setFlipped:YES];
}
-(void)glyphWithData:(const unsigned char *)d size:(NSSize)s view:(RDCView *)v {
int scanline = ((int)s.width + 7) / 8;
data = [[NSData alloc] initWithBytes:d length:scanline * s.height];
planes[0] = planes[1] = (unsigned char *)[data bytes];
bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:planes
pixelsWide:s.width
pixelsHigh:s.height
bitsPerSample:1
samplesPerPixel:2
hasAlpha:YES
isPlanar:YES
colorSpaceName:NSDeviceBlackColorSpace
bytesPerRow:scanline
bitsPerPixel:0];
image = [[NSImage alloc] init];
[image addRepresentation:bitmap];
[image setFlipped:YES];
[self setColor:[[NSColor blackColor] retain]];
}
-(void)cursorWithData:(const unsigned char *)d alpha:(const unsigned char *)a size:(NSSize)s
hotspot:(NSPoint)hotspot view:(RDCView *)v {
int scanline = (int)s.width + 7 / 8;
uint8 *np;
const uint8 *p, *end;
data = [[NSMutableData alloc] initWithCapacity:(int)s.width * (int)s.height * 4];
p = a;
end = a + ((int)s.width * (int)s.height * 3);
np = (uint8 *)[data bytes];
int i = 0, bit;
while (p < end) {
np[0] = p[0];
np[1] = p[1];
np[2] = p[2];
bit = d[i / 8] & (0x80 >> (i % 8));
if (np[0] || np[1] || np[2]) {
if (bit) {
np[0] = np[1] = np[2] = 0;
}
np[3] = 0xff;
} else {
if (!bit) {
np[3] = 0xff;
} else {
np[3] = 0;
}
}
i++;
p += 3;
np += 4;
}
planes[0] = (unsigned char *)[data bytes];
planes[1] = NULL;
bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:planes
pixelsWide:s.width
pixelsHigh:s.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:scanline * 4
bitsPerPixel:0];
image = [[NSImage alloc] init];
[image addRepresentation:bitmap];
[image setFlipped:YES];
cursor = [[NSCursor alloc] initWithImage:image hotSpot:hotspot];
}
-(NSImage *)image {
return image;
}
-(void)dealloc {
[cursor release];
[image release];
[bitmap release];
//[data release];
[color release];
[super dealloc];
}
-(void)setColor:(NSColor *)c {
[c retain];
[color release];
color = c;
}
-(NSColor *)color {
return color;
}
-(NSCursor *)cursor {
return cursor;
}
@end