Guest User

Untitled

a guest
Mar 20th, 2025
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Cocoa/Cocoa.h>
  2. //#import <OsiriXAPI/ROI.h>
  3.  
  4.  
  5. @interface MyPoint : NSObject<NSCoding> {
  6.     NSPoint pt;
  7. }
  8. @property(assign) NSPoint point;
  9. @property(readonly) double x, y;
  10. @end
  11. @implementation MyPoint
  12. @synthesize point = pt;
  13. - (double)x {
  14.     return pt.x;
  15. }
  16. - (double)y {
  17.     return pt.y;
  18. }
  19. - (id)initWithCoder:(NSCoder *)decoder {
  20.     self = [super init];
  21.     if (!self) return nil;
  22.     // 从 NSString 解码 NSPoint
  23.     pt = NSPointFromString([decoder decodeObject]);
  24.     return self;
  25. }
  26. - (void)encodeWithCoder:(NSCoder *)encoder {
  27.     // 将 NSPoint 编码为 NSString
  28.     [encoder encodeObject:NSStringFromPoint(pt)];
  29. }
  30. @end
  31.  
  32.  
  33.  
  34. // Enum definitions copied from original header
  35. typedef enum ROI_mode_ {
  36.     ROI_sleep = 0,
  37.     ROI_drawing = 1,
  38.     ROI_selected = 2,
  39.     ROI_selectedModify = 3
  40. } ROI_mode;
  41.  
  42. typedef enum regionCode_ {
  43.     regionCode_None = 0,
  44.     regionCode_percentage,
  45.     regionCode_dB,
  46.     regionCode_cm,
  47.     regionCode_second,
  48.     regionCode_herz,
  49.     regionCode_dbsec,
  50.     regionCode_cmsec,
  51.     regionCode_cm2,
  52.     regionCode_cm2sec,
  53.     regionCode_cm3,
  54.     regionCode_cm3dsec
  55. } regionCode;
  56.  
  57. typedef enum ToolMode_ {
  58.     tIdle = -1,
  59.     tWL = 0,
  60.     tTranslate,          // 1
  61.     tZoom,               // 2
  62.     tRotate,            // 3
  63.     tNext,              // 4
  64.     tMesure,            // 5
  65.     tROI,               // 6
  66.     t3DRotate,          // 7
  67.     tCross,             // 8
  68.     tOval,              // 9
  69.     tOPolygon,          // 10
  70.     tCPolygon,          // 11
  71.     tAngle,             // 12
  72.     tText,              // 13
  73.     tArrow,             // 14
  74.     tPencil,            // 15
  75.     t3Dpoint,           // 16
  76.     t3DCut,             // 17
  77.     tCamera3D,          // 18
  78.     t2DPoint,           // 19
  79.     tPlain,             // 20
  80.     tBonesRemoval,      // 21
  81.     tWLBlended,         // 22
  82.     tRepulsor,          // 23
  83.     tLayerROI,          // 24
  84.     tROISelector,       // 25
  85.     tAxis,              // 26
  86.     tDynAngle,          // 27
  87.     tCurvedROI,         // 28
  88.     tTAGT,              // 29
  89.     tBall,              // 30
  90.     tOvalAngle,         // 31
  91.     tAutoCurvedROI,     // 32
  92.     t3DPosition,        // 33
  93.     tMagicFill          // 34
  94. } ToolMode;
  95.  
  96. typedef struct RGBAColor {
  97.     float red;
  98.     float green;
  99.     float blue;
  100.     float alpha;
  101. } RGBAColor;
  102.  
  103. //typedef struct RGBColor {
  104. //    unsigned short red;
  105. //    unsigned short green;
  106. //    unsigned short blue;
  107. //} RGBColor;
  108.  
  109. @interface ROI : NSObject <NSCoding>
  110.  
  111. // Properties
  112. @property (nonatomic, retain) NSMutableArray<MyPoint *> *points;
  113. @property (nonatomic, retain) NSMutableArray<NSNumber *> *zPositions;
  114. @property (nonatomic, retain) NSMutableArray<NSString *> *stringTags;
  115. @property (nonatomic, retain) NSMutableArray *ctxArray;
  116. @property (nonatomic, retain) NSMutableArray *textArray;
  117. @property (nonatomic, retain) NSString *name;
  118. @property (nonatomic, retain) NSString *comments;
  119. @property (nonatomic, retain) NSString *savedStudyInstanceUID;
  120. @property (nonatomic, retain) NSString *UUID;
  121. @property (nonatomic, retain) NSString *username;
  122. @property (nonatomic, retain) NSString *layerReferenceFilePath;
  123. @property (nonatomic, retain) NSImage *layerImage;
  124. @property (nonatomic, retain) NSData *layerImageJPEG;
  125. @property (nonatomic, retain) NSColor *layerColor;
  126. @property (nonatomic, retain) NSDate *creationDate;
  127. @property (nonatomic, retain) NSDate *modificationDate;
  128.  
  129. @property (nonatomic, retain) NSString *textualBoxLine1;
  130. @property (nonatomic, retain) NSString *textualBoxLine2;
  131. @property (nonatomic, retain) NSString *textualBoxLine3;
  132. @property (nonatomic, retain) NSString *textualBoxLine4;
  133. @property (nonatomic, retain) NSString *textualBoxLine5;
  134. @property (nonatomic, retain) NSString *textualBoxLine6;
  135. @property (nonatomic, retain) NSString *textualBoxLine7;
  136. @property (nonatomic, retain) NSString *textualBoxLine8;
  137. @property (nonatomic, retain) NSString *textualBoxLine9;
  138. @property (nonatomic, retain) NSString *textualBoxLine10;
  139.  
  140. @property (nonatomic) NSRect rect;
  141. @property (nonatomic) NSPoint imageOrigin;
  142. @property (nonatomic) NSPoint previousPoint;
  143. @property (nonatomic) NSPoint clickPoint;
  144.  
  145. @property (nonatomic) float thickness;
  146. @property (nonatomic) float opacity;
  147. @property (nonatomic) float offsetTextBox_x;
  148. @property (nonatomic) float offsetTextBox_y;
  149. @property (nonatomic) float pixelSpacingX;
  150. @property (nonatomic) float pixelSpacingY;
  151. @property (nonatomic) float mousePosMeasure;
  152. @property (nonatomic) float ovalAngle1;
  153. @property (nonatomic) float ovalAngle2;
  154. @property (nonatomic) float roiRotation;
  155. @property (nonatomic) float zLocation;
  156. @property (nonatomic) float preferredWL;
  157. @property (nonatomic) float preferredWW;
  158. @property (nonatomic) float layerPixelSpacingX;
  159. @property (nonatomic) float layerPixelSpacingY;
  160.  
  161. @property (nonatomic) int textureWidth;
  162. @property (nonatomic) int textureHeight;
  163. @property (nonatomic) int textureUpLeftCornerX;
  164. @property (nonatomic) int textureUpLeftCornerY;
  165. @property (nonatomic) int textureDownRightCornerX;
  166. @property (nonatomic) int textureDownRightCornerY;
  167. @property (nonatomic) int pointUnderMouse;
  168. @property (nonatomic) int originalIndexForAlias;
  169. @property (nonatomic) int _calciumThreshold;
  170. @property (nonatomic) void *textureBuffer;
  171.  
  172. @property (nonatomic) long selectedModifyPoint;
  173.  
  174. @property (nonatomic) double creationTimeStamp;
  175. @property (nonatomic) double groupID;
  176.  
  177. @property (nonatomic) BOOL needQuartz;
  178. @property (nonatomic) BOOL fill;
  179. @property (nonatomic) BOOL clickInTextBox;
  180. @property (nonatomic) BOOL locked;
  181. @property (nonatomic) BOOL selectable;
  182. @property (nonatomic) BOOL isAliased;
  183. @property (nonatomic) BOOL displayTextualData;
  184. @property (nonatomic) BOOL mouseOverROI;
  185. @property (nonatomic) BOOL canColorizeLayer;
  186. @property (nonatomic) BOOL isLayerOpacityConstant;
  187. @property (nonatomic) BOOL canResizeLayer;
  188. @property (nonatomic) BOOL _displayCalciumScoring;
  189. @property (nonatomic) BOOL _hasIsSpline;
  190. @property (nonatomic) BOOL _isSpline;
  191. @property (nonatomic) BOOL dontDisplayInKeyImagesWindow;
  192. @property (nonatomic) BOOL dontSavePreferredWLWW;
  193. @property (nonatomic) BOOL is3DMeasure;
  194. @property (nonatomic) BOOL textNameOnly;
  195.  
  196. @property (nonatomic) ToolMode type;
  197. @property (nonatomic) ROI_mode mode;
  198. @property (nonatomic) ROI_mode previousMode;
  199. @property (nonatomic) RGBColor color;
  200.  
  201. // Class methods, return ROI or NSArray<ROI>
  202. + (id)unarchiveObjectWithData:(NSData *)data;
  203. + (id)unarchiveObjectWithFile:(NSString *)path;
  204.  
  205. @end
  206.  
  207.  
  208.  
  209. @implementation ROI
  210.  
  211. #pragma mark - NSCoding
  212.  
  213. - (instancetype)initWithCoder:(NSCoder *)coder {
  214.     self = [super init];
  215.     if (!self) return nil;
  216.  
  217.     if ([coder isKindOfClass:[NSKeyedUnarchiver class]]) {
  218.         // Use NSKeyedUnarchiver format
  219.         self.points = [coder decodeObjectForKey:@"points"];
  220.         self.rect = [coder decodeRectForKey:@"rect"];
  221.         self.type = (ToolMode)[coder decodeFloatForKey:@"type"];
  222.         self.needQuartz = [coder decodeFloatForKey:@"needQuartz"] != 0;
  223.         self.thickness = [coder decodeFloatForKey:@"thickness"];
  224.         self.fill = [coder decodeFloatForKey:@"fill"] != 0;
  225.         self.opacity = [coder decodeFloatForKey:@"opacity"];
  226.        
  227.         // Decode color
  228.         self->_color.red = (unsigned short)[coder decodeFloatForKey:@"color.red"];
  229.         self->_color.green = (unsigned short)[coder decodeFloatForKey:@"color.green"];
  230.         self->_color.blue = (unsigned short)[coder decodeFloatForKey:@"color.blue"];
  231.        
  232.         self.name = [coder decodeObjectForKey:@"name"];
  233.         self.comments = [coder decodeObjectForKey:@"comments"];
  234.         self.pixelSpacingX = [coder decodeFloatForKey:@"pixelSpacingX"];
  235.         self.imageOrigin = [coder decodePointForKey:@"imageOrigin"];
  236.        
  237.         if ([coder containsValueForKey:@"pixelSpacingY"]) {
  238.             self.pixelSpacingY = [coder decodeFloatForKey:@"pixelSpacingY"];
  239.         } else {
  240.             self.pixelSpacingY = self.pixelSpacingX;
  241.         }
  242.        
  243.         // Handle plain ROI texture data
  244.         if (self.type == tPlain) {
  245.             self.textureWidth = [coder decodeIntForKey:@"textureWidth"];
  246.             self.textureHeight = [coder decodeIntForKey:@"textureHeight"];
  247.             self.textureUpLeftCornerX = [coder decodeIntForKey:@"textureUpLeftCornerX"];
  248.             self.textureUpLeftCornerY = [coder decodeIntForKey:@"textureUpLeftCornerY"];
  249.             self.textureDownRightCornerX = [coder decodeIntForKey:@"textureDownRightCornerX"];
  250.             self.textureDownRightCornerY = [coder decodeIntForKey:@"textureDownRightCornerY"];
  251.            
  252.             NSData *textureData = [coder decodeObjectForKey:@"textureBuffer"];
  253.             if (textureData) {
  254.                 self->_textureBuffer = malloc(self.textureHeight * self.textureWidth);
  255.                 memcpy(self->_textureBuffer, textureData.bytes, self.textureHeight * self.textureWidth);
  256.             }
  257.         }
  258.        
  259.         // Decode z positions array
  260.         if ([coder containsValueForKey:@"zPositions"]) {
  261.             self.zPositions = [coder decodeObjectForKey:@"zPositions"];
  262.         } else {
  263.             self.zPositions = [NSMutableArray array];
  264.         }
  265.        
  266.         // Decode text box offsets
  267.         self.offsetTextBox_x = [coder decodeFloatForKey:@"offsetTextBox_x"];
  268.         self.offsetTextBox_y = [coder decodeFloatForKey:@"offsetTextBox_y"];
  269.        
  270.         // Decode calcium scoring
  271.         self._calciumThreshold = [coder decodeIntForKey:@"_calciumThreshold"];
  272.         self._displayCalciumScoring = [coder decodeBoolForKey:@"_displayCalciumScoring"];
  273.        
  274.         // Decode group ID
  275.         self.groupID = [coder decodeDoubleForKey:@"groupID"];
  276.        
  277.         // Handle layer ROI data
  278.         if (self.type == tLayerROI) {
  279.             self.layerImageJPEG = [coder decodeObjectForKey:@"layerImageJPEG"];
  280.             self.layerImage = [[NSImage alloc] initWithData:self.layerImageJPEG];
  281.         }
  282.        
  283.         // Decode textual box lines
  284.         self.textualBoxLine1 = [coder decodeObjectForKey:@"textualBoxLine1"];
  285.         self.textualBoxLine2 = [coder decodeObjectForKey:@"textualBoxLine2"];
  286.         self.textualBoxLine3 = [coder decodeObjectForKey:@"textualBoxLine3"];
  287.         self.textualBoxLine4 = [coder decodeObjectForKey:@"textualBoxLine4"];
  288.         self.textualBoxLine5 = [coder decodeObjectForKey:@"textualBoxLine5"];
  289.         self.textualBoxLine6 = [coder decodeObjectForKey:@"textualBoxLine6"];
  290.         self.textualBoxLine7 = [coder decodeObjectForKey:@"textualBoxLine7"];
  291.         self.textualBoxLine8 = [coder decodeObjectForKey:@"textualBoxLine8"];
  292.         self.textualBoxLine9 = [coder decodeObjectForKey:@"textualBoxLine9"];
  293.        
  294.         // Decode layer properties
  295.         self.isLayerOpacityConstant = [coder decodeBoolForKey:@"isLayerOpacityConstant"];
  296.         self.canColorizeLayer = [coder decodeBoolForKey:@"canColorizeLayer"];
  297.         self.layerColor = [coder decodeObjectForKey:@"layerColor"];
  298.        
  299.         // Decode display properties
  300.         if ([coder containsValueForKey:@"displayTextualData"]) {
  301.             self.displayTextualData = [coder decodeBoolForKey:@"displayTextualData"];
  302.         } else {
  303.             self.displayTextualData = YES;
  304.         }
  305.        
  306.         self.canResizeLayer = [coder decodeBoolForKey:@"canResizeLayer"];
  307.        
  308.         if ([coder containsValueForKey:@"selectable"]) {
  309.             self.selectable = [coder decodeBoolForKey:@"selectable"];
  310.         } else {
  311.             self.selectable = YES;
  312.         }
  313.        
  314.         self.locked = [coder decodeBoolForKey:@"locked"];
  315.         self.isAliased = [coder decodeBoolForKey:@"isAliased"];
  316.         self._isSpline = [coder decodeBoolForKey:@"_isSpline"];
  317.         self._hasIsSpline = [coder decodeBoolForKey:@"_hasIsSpline"];
  318.        
  319.         // Decode study instance UID
  320.         self.savedStudyInstanceUID = [[coder decodeObjectForKey:@"savedStudyInstanceUID"] copy];
  321.        
  322.         // Handle oval angles for oval ROI type
  323.         if (self.type == tOvalAngle) {
  324.             self.ovalAngle1 = [coder decodeFloatForKey:@"ovalAngle1"];
  325.             self.ovalAngle2 = [coder decodeFloatForKey:@"ovalAngle2"];
  326.         }
  327.        
  328.         // Decode rotation and z location
  329.         self.roiRotation = [coder decodeFloatForKey:@"roiRotation"];
  330.         if ([coder containsValueForKey:@"zLocation"]) {
  331.             self.zLocation = [coder decodeFloatForKey:@"zLocation"];
  332.         }
  333.        
  334.         // Decode string tags and display flags
  335.         self.stringTags = [[coder decodeObjectForKey:@"stringTags"] mutableCopy];
  336.         self.dontDisplayInKeyImagesWindow = [coder decodeBoolForKey:@"dontDisplayInKeyImagesWindow"];
  337.        
  338.         // Decode window level/width preferences
  339.         self.preferredWL = [coder decodeFloatForKey:@"preferredWL"];
  340.         self.preferredWW = [coder decodeFloatForKey:@"preferredWW"];
  341.         self.dontSavePreferredWLWW = [coder decodeBoolForKey:@"dontSavePreferredWLWW"];
  342.        
  343.         // Decode measurement flags
  344.         self.is3DMeasure = [coder decodeBoolForKey:@"is3DMeasure"];
  345.        
  346.         // Decode metadata
  347.         self.UUID = [coder decodeObjectForKey:@"UUID"];
  348.         self.username = [coder decodeObjectForKey:@"username"];
  349.         self.creationDate = [coder decodeObjectForKey:@"creationDate"];
  350.         self.modificationDate = [coder decodeObjectForKey:@"modificationDate"];
  351.         self.textNameOnly = [coder decodeBoolForKey:@"textNameOnly"];
  352.        
  353.         self.originalIndexForAlias = [coder decodeIntForKey:@"originalIndexForAlias"];
  354.        
  355.     } else {
  356.         // Legacy unarchive format (not implemented)
  357.         NSLog(@"Legacy unarchive format not supported");
  358.         return nil;
  359.     }
  360.    
  361.     // Initialize other properties
  362.     self.mode = ROI_sleep;
  363.     self.previousPoint = NSMakePoint(-1000, -1000);
  364.     self.mousePosMeasure = -1;
  365.     self.ctxArray = [NSMutableArray arrayWithCapacity:10];
  366.     self.textArray = [NSMutableArray arrayWithCapacity:10];
  367.    
  368.     if (!self.stringTags) {
  369.         self.stringTags = [NSMutableArray arrayWithCapacity:10];
  370.     }
  371.    
  372.     if (!self.creationTimeStamp) {
  373.         self.creationTimeStamp = [NSDate timeIntervalSinceReferenceDate];
  374.     }
  375.    
  376.     return self;
  377. }
  378.  
  379. - (NSDictionary *)toJSONDictionary {
  380.     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  381.    
  382.     // 基本属性
  383.     dict[@"type"] = @(self.type);
  384.     dict[@"name"] = self.name ?: @"";
  385.     dict[@"comments"] = self.comments ?: @"";
  386.     dict[@"rect"] = @{
  387.         @"x": @(self.rect.origin.x),
  388.         @"y": @(self.rect.origin.y),
  389.         @"width": @(self.rect.size.width),
  390.         @"height": @(self.rect.size.height)
  391.     };
  392.    
  393.     // 点数组
  394.     NSMutableArray *pointsArray = [NSMutableArray array];
  395.     for (MyPoint *point in self.points) {
  396.         [pointsArray addObject:@{
  397.             @"x": @(point.x),
  398.             @"y": @(point.y)
  399.         }];
  400.     }
  401.     dict[@"points"] = pointsArray;
  402.    
  403.     // 颜色
  404.     dict[@"color"] = @{
  405.         @"red": @(self.color.red),
  406.         @"green": @(self.color.green),
  407.         @"blue": @(self.color.blue)
  408.     };
  409.    
  410.     // 图层属性
  411.     dict[@"thickness"] = @(self.thickness);
  412.     dict[@"opacity"] = @(self.opacity);
  413.     dict[@"fill"] = @(self.fill);
  414.     dict[@"needQuartz"] = @(self.needQuartz);
  415.    
  416.     // 空间属性
  417.     dict[@"pixelSpacingX"] = @(self.pixelSpacingX);
  418.     dict[@"pixelSpacingY"] = @(self.pixelSpacingY);
  419.     dict[@"imageOrigin"] = @{
  420.         @"x": @(self.imageOrigin.x),
  421.         @"y": @(self.imageOrigin.y)
  422.     };
  423.     dict[@"zLocation"] = @(self.zLocation);
  424.    
  425.     // ROI 状态
  426.     dict[@"locked"] = @(self.locked);
  427.     dict[@"selectable"] = @(self.selectable);
  428.     dict[@"displayTextualData"] = @(self.displayTextualData);
  429.    
  430.     // 文本框
  431.     dict[@"textualBoxLines"] = @{
  432.         @"line1": self.textualBoxLine1 ?: @"",
  433.         @"line2": self.textualBoxLine2 ?: @"",
  434.         @"line3": self.textualBoxLine3 ?: @"",
  435.         @"line4": self.textualBoxLine4 ?: @"",
  436.         @"line5": self.textualBoxLine5 ?: @"",
  437.         @"line6": self.textualBoxLine6 ?: @"",
  438.         @"line7": self.textualBoxLine7 ?: @"",
  439.         @"line8": self.textualBoxLine8 ?: @"",
  440.         @"line9": self.textualBoxLine9 ?: @"",
  441.         @"line10": self.textualBoxLine10 ?: @""
  442.     };
  443.    
  444.     // 元数据
  445.     dict[@"UUID"] = self.UUID ?: @"";
  446.     dict[@"username"] = self.username ?: @"";
  447.     dict[@"creationDate"] = self.creationDate ? [self.creationDate description] : @"";
  448.     dict[@"modificationDate"] = self.modificationDate ? [self.modificationDate description] : @"";
  449.     dict[@"creationTimeStamp"] = @(self.creationTimeStamp);
  450.    
  451.     return dict;
  452. }
  453.  
  454. #pragma mark - Class Methods
  455.  
  456. + (id)unarchiveObjectWithData:(NSData *)data {
  457.     if (!data.length) return nil;
  458.    
  459.     // Try NSKeyedUnarchiver first
  460.     id obj = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  461.     if (obj) return obj;
  462.    
  463.     // Try checking for legacy format
  464.     char lastByte;
  465.     [data getBytes:&lastByte range:NSMakeRange(data.length - 1, 1)];
  466.     if (lastByte) {
  467.         // Try NSUnarchiver
  468.         return [NSUnarchiver unarchiveObjectWithData:data];
  469.     }
  470.    
  471.     // Try NSKeyedUnarchiver without last byte
  472.     NSData *truncatedData = [data subdataWithRange:NSMakeRange(0, data.length - 1)];
  473.     obj = [NSKeyedUnarchiver unarchiveObjectWithData:truncatedData];
  474.     if (obj) return obj;
  475.    
  476.     // Try NSUnarchiver as last resort
  477.     return [NSUnarchiver unarchiveObjectWithData:data];
  478. }
  479.  
  480. + (id)unarchiveObjectWithFile:(NSString *)path {
  481.     NSData *data = [NSData dataWithContentsOfFile:path];
  482.     return [self unarchiveObjectWithData:data];
  483. }
  484.  
  485. @end
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492. int main(int argc, const char * argv[]) {
  493.     if (argc != 2) {
  494.         printf("Usage: %s <roi_file_path>\n", argv[0]);
  495.         return 1;
  496.     }
  497.    
  498.     NSString *path = [NSString stringWithUTF8String:argv[1]];
  499.     ROI *roi = [ROI unarchiveObjectWithFile:path];
  500.     if ([roi isKindOfClass:NSArray.class]) {
  501.         roi = [(NSArray *)roi firstObject];
  502.     }
  503.    
  504.     if (!roi) {
  505.         printf("Failed to load ROI file.\n");
  506.         return 1;
  507.     }
  508.    
  509.     // 转换为 JSON 并打印
  510.     NSDictionary *jsonDict = [roi toJSONDictionary];
  511.     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict
  512.                                                       options:NSJSONWritingPrettyPrinted
  513.                                                         error:nil];
  514.     NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  515.     printf("%s\n", [jsonString UTF8String]);
  516.    
  517.     return 0;
  518. }
  519.  
Add Comment
Please, Sign In to add comment