Skip to content

Commit

Permalink
Export masks for registration in python
Browse files Browse the repository at this point in the history
  • Loading branch information
raulcatena committed Apr 12, 2020
1 parent d0c8e61 commit 9ba9ad4
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 6 deletions.
5 changes: 5 additions & 0 deletions 3DIMC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@
0D6720511E3FF3C800C19663 /* IMCTransformDictController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IMCTransformDictController.h; sourceTree = "<group>"; };
0D6720521E3FF3C800C19663 /* IMCTransformDictController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IMCTransformDictController.m; sourceTree = "<group>"; };
0D6720541E3FF3E400C19663 /* IMCTransformDictController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = IMCTransformDictController.xib; sourceTree = "<group>"; };
0D6AFD87244288E700B9BF38 /* histoCAT++3D.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "histoCAT++3D.entitlements"; sourceTree = "<group>"; };
0D6F4F2A1E473CC200533127 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
0D7CB26B217755F200EA36A5 /* License-AGPL3.0.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "License-AGPL3.0.txt"; sourceTree = "<group>"; };
0D7CB26F217756EB00EA36A5 /* SOFTWARE CONTRIBUTION AGREEMENT-UZH.doc */ = {isa = PBXFileReference; lastKnownFileType = file; path = "SOFTWARE CONTRIBUTION AGREEMENT-UZH.doc"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1524,6 +1525,7 @@
0DB37CD21E304D25002975E2 = {
isa = PBXGroup;
children = (
0D6AFD87244288E700B9BF38 /* histoCAT++3D.entitlements */,
0D7CB26F217756EB00EA36A5 /* SOFTWARE CONTRIBUTION AGREEMENT-UZH.doc */,
0D7CB26B217755F200EA36A5 /* License-AGPL3.0.txt */,
0D57600E1EB9CAC2008CAB94 /* Frameworks */,
Expand Down Expand Up @@ -1933,6 +1935,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -2762,6 +2765,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = "histoCAT++3D.entitlements";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = HA3BXXH796;
Expand Down Expand Up @@ -2825,6 +2829,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = "histoCAT++3D.entitlements";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = HA3BXXH796;
Expand Down
1 change: 1 addition & 0 deletions 3DIMC/IMCComputationOnMask.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
-(void)addFeaturesFromCellProfiler:(NSURL *)url;
-(void)extractDataForMask:(NSIndexSet *)computations processedData:(BOOL)rawOrProcessedData;
-(float *)createImageForMaskWithCellData:(float *)data maskOption:(MaskOption)option maskType:(MaskType)maskType maskSingleColor:(NSColor *)maskSingleColor;
-(UInt8 *)createImageForCategoricalMaskWithCellDataIndex:(NSUInteger)index maskType:(MaskType)maskType;
-(CGImageRef)coloredMaskForChannel:(NSInteger)channel color:(NSColor *)color maskOption:(MaskOption)maskOption maskType:(MaskType)maskType maskSingleColor:(NSColor *)maskSingleColor brightField:(BOOL)brightField;
-(BOOL)hasBackData;
-(void)saveData;
Expand Down
45 changes: 45 additions & 0 deletions 3DIMC/IMCComputationOnMask.m
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,51 @@ -(float *)createImageForMaskWithCellData:(float *)data maskOption:(MaskOption)op
return img;
}

-(UInt8 *)createImageForCategoricalMaskWithCellDataIndex:(NSUInteger)index maskType:(MaskType)maskType{
if(!self.mask.mask)
return NULL;

int * copy = copyMask(self.mask.mask, (int)self.mask.imageStack.width, (int)self.mask.imageStack.height);
UInt8 * img = calloc(self.mask.imageStack.numberOfPixels, sizeof(UInt8));

if(self.computedData == NULL)
return NULL;
float * data = self.computedData[index];

NSInteger pix = self.mask.imageStack.numberOfPixels;

for (NSInteger i = 0; i < pix; i++) {
if(copy[i] == 0)continue;
NSInteger index = abs(copy[i]) - 1;


if(maskType == MASK_ALL_CELL){
img[i] = data[index];
copy[i] = abs(copy[i]);
}

else if(maskType == MASK_CYT){
img[i] = MAX((copy[i] > 0) * data[index], 0);
copy[i] = copy[i] > 0?copy[i]:0;
}

else if(maskType == MASK_NUC){
img[i] = MAX((copy[i] < 0) * data[index], 0);
copy[i] = copy[i] < 0?-copy[i]:0;
}
else if(maskType == MASK_NUC_PLUS_CYT){
img[i] = (copy[i]/abs(copy[i])) * data[index];
}
}

for (NSInteger i = 0; i < pix; i++)
if(copy[i] == 0)
img[i] = 0;

free(copy);
return img;
}

-(CGImageRef)coloredMaskForChannel:(NSInteger)channel color:(NSColor *)color maskOption:(MaskOption)option maskType:(MaskType)maskType maskSingleColor:(NSColor *)maskSingleColor brightField:(BOOL)brightField{
if(channel == NSNotFound)
return NULL;
Expand Down
4 changes: 4 additions & 0 deletions 3DIMC/IMCFileExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@class IMCScrollView;
@class IMCComputationOnMask;
@class IMCLoader;
@class IMCPixelClassification;

@interface IMCFileExporter : NSObject

Expand All @@ -26,6 +27,9 @@
//this is a subroutine, but useful to expose
+(void)writeArrayOfRefImages:(NSArray *)images withTitles:(NSArray *)titles atPath:(NSString *)path in16bits:(BOOL)sixteenBits;//Array of ImageRefs multipage

//Mask saving
+(void)saveMask:(IMCComputationOnMask *)mask channel:(NSInteger)channel path:(NSString *)path;

//JPEG quick saving
+(void)saveJPEGFromScroll:(IMCScrollView *)scroll withPath:(NSString *)fullPath allOrZoomed:(BOOL)zoomed;
+(void)copyToClipBoardFromScroll:(IMCScrollView *)scroll allOrZoomed:(BOOL)zoomed;
Expand Down
19 changes: 16 additions & 3 deletions 3DIMC/IMCFileExporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ +(void)writer:(TIFF *)tiff writeBuffer:(unsigned char *)buffer width:(int)width
TIFFSetField(tiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
}

int bytesPix = 32/bitsPixel;//Improve RCF

int bytesPix = 8/bitsPixel;//Improve RCF
for (int i = 0; i < height; i++) {

TIFFWriteScanline(tiff, &buffer[i * width * bytesPix], i, 0);//1 because there is always a leading 0
}

Expand Down Expand Up @@ -174,6 +172,21 @@ +(void)saveMultipageTiffAllChannels:(IMCImageStack *)stack path:(NSString *)path
[IMCFileExporter writeArrayOfRefImages:arr withTitles:stack.channels atPath:path in16bits:YES];
}

+(void)saveMask:(IMCComputationOnMask *)mask channel:(NSInteger)channel path:(NSString *)path{

UInt8 * bytes = [mask createImageForCategoricalMaskWithCellDataIndex:channel maskType:MASK_ALL_CELL];

TIFF *writer = TIFFOpen(path.UTF8String, "w");

NSInteger bitsPerPixel = 8;
int samples = 1;

[self writer:writer writeBuffer:bytes width:(int)mask.mask.imageStack.width height:(int)mask.mask.imageStack.height page:0 bpPixel:(int)bitsPerPixel samplesPerPixel:samples totalPages:1 imageName:[mask.channels objectAtIndex:channel]];

TIFFClose(writer);
free(bytes);
}

+(NSImage *)getNSImageForIMCScrollView:(IMCScrollView *)scroll zoomed:(BOOL)zoomed{
NSImage *someImage;
if(zoomed)
Expand Down
9 changes: 7 additions & 2 deletions 3DIMC/IMCFileWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ -(void)populateJsonDictForSingleImageFile:(NSString *)path success:(BOOL *)succe
}
else
imageStack = (IMCImageStack *)self.children.firstObject.children.firstObject;
NSLog(@"%@", path);

if([imageStack hasTIFFBackstore]){
path = [imageStack backStoreTIFFPath];
NSLog(@"%@", path);

}
NSData *data = [NSData dataWithContentsOfFile:path];
NSError * error = nil;
data = [NSData dataWithContentsOfFile:path options:0 error:&error];
if(error)
NSLog(@"Error ---- > %@ %@", error, error.userInfo);
NSLog(@"%@",data);
[self loadSingleFiler:imageStack data:data path:path success:success];
}

Expand Down
2 changes: 1 addition & 1 deletion 3DIMC/IMCImageGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ +(CGImageRef)imageRefWithArrayOfCGImages:(NSMutableArray *)array width:(NSIntege
CGImageRef refi = (__bridge CGImageRef)last;
CGRect framePaint = CGRectMake(0, 0, width, height);
CGContextDrawImage(canvas, framePaint, refi);
CGImageRelease(refi);
// CGImageRelease(refi);
}

//ref = CGBitmapContextCreateImage (canvas); //This leaked a lot changed to...
Expand Down
4 changes: 4 additions & 0 deletions 3DIMC/IMCPixelClassification.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ -(BOOL)loadMask{
return NO;
}

-(BOOL)saveMaskForExternalRegistration{
return YES;
}

-(BOOL)loadNuclearMask{
if(!self.secondRelativePath)
self.secondRelativePath =
Expand Down
34 changes: 34 additions & 0 deletions 3DIMC/IMCWorkSpace.mm
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,40 @@ -(IBAction)saveMultiPageTIFFs:(NSButton *)sender{
}
}];
}

-(IBAction)savePixelClassificationsAsTIFFs:(NSButton *)sender{


NSOpenPanel *panel = [NSOpenPanel openPanel];
panel.canChooseFiles = NO;
panel.canChooseDirectories = YES;
panel.canCreateDirectories = YES;
[panel beginSheetModalForWindow:self.windowForSheet completionHandler:^(NSInteger result){
if (result == NSModalResponseOK)
for (IMCComputationOnMask *comp in self.inScopeComputations.copy) {
BOOL wasLoaded = comp.isLoaded;
if(!wasLoaded)
[comp loadLayerDataWithBlock:nil];
while (!comp.isLoaded);
if([panel.URL.path isEqualToString:self.dataCoordinator.filePath])
{
[General runAlertModalWithMessage:@"Choose a directory different to the main project directory"];
}else if(self.channels.selectedRow == NSNotFound){
[General runAlertModalWithMessage:@"One channel must be selected"];
}
else
{
NSLog(@"____");
[IMCFileExporter saveMask:comp channel:self.channels.selectedRow path:[panel.URL.path
stringByAppendingPathComponent:[comp.mask.imageStack backStoreTIFFPath].lastPathComponent]];

if(!wasLoaded)
[comp unLoadLayerDataWithBlock:nil];
}
}
}];
}

-(IBAction)saveMultiPageTIFFsWithSelected:(NSButton *)sender{

if(![self checkThereIsImageInScopeAndChannelsSelected:YES])
Expand Down
2 changes: 2 additions & 0 deletions 3DIMC/IMCWorkspaceSelector.m
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,12 @@ -(NSMenu *)tableView:(NSTableView *)aTableView menuForRows:(NSIndexSet *)rows{

NSArray *titles = @[@"Export FCS file...",
@"Add features from Cell Profiler file...",
@"Save mask..."
//@"Convert to mask"
];
NSArray *selectors = @[@"exportFCSFile...",
@"addFeaturesFromCP:",
@"savePixelClassificationsAsTIFFs:"
// @"convertToMask:"
];
[addingTitles addObjectsFromArray:titles];
Expand Down
5 changes: 5 additions & 0 deletions histoCAT++3D.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

0 comments on commit 9ba9ad4

Please sign in to comment.