diff --git a/ij/gui/Roi.java b/ij/gui/Roi.java index 4d5e76016..61d5c4703 100644 --- a/ij/gui/Roi.java +++ b/ij/gui/Roi.java @@ -55,6 +55,7 @@ public class Roi extends Object implements Cloneable, java.io.Serializable, Iter public static final int FERET_ARRAYSIZE = 16; // Size of array with Feret values public static final int FERET_ARRAY_POINTOFFSET = 8; // Where point coordinates start in Feret array private static final String NAMES_KEY = "group.names"; + private static final int MAX_ROI_GROUP = 65535; // limit to uint16 max value static final int NO_MODS=0, ADD_TO_ROI=1, SUBTRACT_FROM_ROI=2; // modification states @@ -1807,7 +1808,7 @@ public static int getDefaultGroup() { * @see #getGroupColor */ public static void setDefaultGroup(int group) { - if (group<0 || group>255) + if (group<0 || group>MAX_ROI_GROUP) throw new IllegalArgumentException("Invalid group: "+group); defaultGroup = group; groupColor = getGroupColor(group); @@ -1820,7 +1821,7 @@ public int getGroup() { /** Returns the group name associtated with the specified group. */ public static String getGroupName(int groupNumber) { - if (groupNumber<1 || groupNumber>255) + if (groupNumber<1 || groupNumber>MAX_ROI_GROUP) return null; if (groupNames==null && groupNamesString==null) return null; @@ -1835,7 +1836,7 @@ public static String getGroupName(int groupNumber) { } public static synchronized void setGroupName(int groupNumber, String name) { - if (groupNumber<1 || groupNumber>255) + if (groupNumber<1 || groupNumber>MAX_ROI_GROUP) return; if (groupNamesString==null && groupNames==null) groupNames = new String[groupNumber]; @@ -1884,7 +1885,7 @@ public static void setGroupNames(String names) { /** Sets the group of this Roi, and updates stroke color accordingly. */ public void setGroup(int group) { - if (group<0 || group>255) + if (group<0 || group>MAX_ROI_GROUP) throw new IllegalArgumentException("Invalid group: "+group); if (group>0) setStrokeColor(getGroupColor(group)); diff --git a/ij/io/RoiDecoder.java b/ij/io/RoiDecoder.java index 2d989f008..9efc822c3 100644 --- a/ij/io/RoiDecoder.java +++ b/ij/io/RoiDecoder.java @@ -89,6 +89,7 @@ public class RoiDecoder { public static final int ROI_PROPS_OFFSET = 40; public static final int ROI_PROPS_LENGTH = 44; public static final int COUNTERS_OFFSET = 48; + public static final int GROUP_EXTENDED = 52; //short (uint16) for groups > 255 // subtypes public static final int TEXT = 1; @@ -207,7 +208,11 @@ public Roi getRoi() throws IOException { overlayFontSize = getShort(hdr2Offset+OVERLAY_FONT_SIZE); imageOpacity = getByte(hdr2Offset+IMAGE_OPACITY); imageSize = getInt(hdr2Offset+IMAGE_SIZE); - group = getByte(hdr2Offset+GROUP); + int groupByte = getByte(hdr2Offset+GROUP); + if (version>=229 && groupByte==0 && hdr2Offset+GROUP_EXTENDED+2<=size) + group = getUnsignedShort(hdr2Offset+GROUP_EXTENDED); + else + group = groupByte; } if (name!=null && name.endsWith(".roi")) diff --git a/ij/io/RoiEncoder.java b/ij/io/RoiEncoder.java index 745ba1197..9bf1e156f 100644 --- a/ij/io/RoiEncoder.java +++ b/ij/io/RoiEncoder.java @@ -15,7 +15,7 @@ public class RoiEncoder { static final int HEADER_SIZE = 64; static final int HEADER2_SIZE = 64; - static final int VERSION = 228; // v1.52t (roi groups, scale stroke width) + static final int VERSION = 229; // v1.52u (extended roi groups via uint16) private String path; private OutputStream f; private final int polygon=0, rect=1, oval=2, line=3, freeline=4, polyline=5, noRoi=6, freehand=7, @@ -423,7 +423,13 @@ void putHeader2(Roi roi, int hdr2Offset) { putProps(roi, hdr2Offset); if (countersSize>0) putPointCounters(roi, hdr2Offset); - putByte(hdr2Offset+RoiDecoder.GROUP, roi.getGroup()); + int group = roi.getGroup(); + if (group > 255) { + putByte(hdr2Offset+RoiDecoder.GROUP, 0); // marker for extended group + putShort(hdr2Offset+RoiDecoder.GROUP_EXTENDED, group); + } else { + putByte(hdr2Offset+RoiDecoder.GROUP, group); + } } void putName(Roi roi, int hdr2Offset) {