Skip to content

Instantly share code, notes, and snippets.

@dennda
dennda / gist:1212608
Created September 12, 2011 22:10
CFSocket: No accept callback? Here's the problem:
// Doesn't work:
readsock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM,
IPPROTO_TCP, kCFSocketReadCallBack |
kCFSocketAcceptCallBack | kCFSocketDataCallBack |
kCFSocketConnectCallBack | kCFSocketWriteCallBack,
(CFSocketCallBack)&readSockCallBack, info);
// Works:
readsock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM,
IPPROTO_TCP, kCFSocketAcceptCallBack,
@dennda
dennda / gist:1212964
Created September 13, 2011 01:52
Send & Receive
// Send:
const uint8_t query = (uint8_t)USERNAME_QUERY;
const uint8_t length = (uint8_t)sizeof(query);
[stream write:&length maxLength:sizeof(uint8_t)];
[stream write:&query maxLength:length];
// Receive:
uint8_t length;
[inStream read:&length maxLength:sizeof(uint8_t)];
uint16_t pix;
uint16_t min = 34464;
uint16_t max = 0;
uint16_t *d = (uint16_t*) imgData;
for (int i = 0; i < 512 * 512; i++) {
pix = d[i];
if (pix > max)
max = pix;
else if (pix < min)
min = pix;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef) data);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGImageRef img = CGImageCreate(width, height, bits_allocated, bits_allocated,
width,// * (bits_allocated / 8),
colorspace,
kCGBitmapByteOrderDefault,
provider, NULL, YES,
kCGRenderingIntentDefault
);
uint16_t pix = 0;
uint16_t min = 34464;
uint16_t max = 0;
uint16_t *d = (uint16_t*) imgData;
for (int i = 0; i < 512 * 512; i++) {
pix = d[i];
if (pix > max)
max = pix;
else if (pix < min)
min = pix;
// In the interface:
@interface TransferFunction : NSObject {
id <TransferFunctionDrawingDelegate> _delegate;
}
@property (nonatomic, weak) id <TransferFunctionDrawingDelegate> delegate;
// In the implementation:
@synthesize delegate=setDelegate;
- (void)setDelegate:(id <TransferFunctionDrawingDelegate>)delegate {
uint16_t *remapped = (uint16_t*) malloc(width * height * sizeof(uint16_t));
vImage_Buffer src = {imgData,
(vImagePixelCount) height,
(vImagePixelCount) width,
bytes_per_row
};
vImage_Buffer dst = {remapped,
(vImagePixelCount) height,
(vImagePixelCount) width,
bytes_per_row
NSMutableArray *histo = [NSMutableArray array];
for (int i = 0; i < num_bins; i++)
[histo insertObject:[NSNumber numberWithUnsignedLong:histogram[i]] atIndex:i];
uint8 *fourchan = (uint8*) malloc(sizeof(uint8) * width * height * 4);
short cur;
short *subscript = (short*) imgData;
for (int i = 0; i < width * height; i++) {
cur = (subscript[i] + 1000) / (int) pow(2., sizeof(short)/2.);
fourchan[i*4] = (uint8) cur;
}
uint8 *fourchan = (uint8*) malloc(sizeof(uint8) * width * height * 4);
short cur;
short *subscript = (short*) imgData;
for (int i = 0; i < width * height; i++) {
cur = subscript[i] + 1000;
fourchan[i*4] = cur;
}