macro "Split to Strips" { /* A simple macro to split a large file into smaller blocks v220728: 1st version f1-f2: updated stripKnownExtensionFromString function. v230803: Replaced get for 1.54g10 f1: function pathOverwriteCheck fix v231128: Removed "!" from showStatus for stability. F1 updated function pathOverwriteCheck */ macroL = "Split_To_Strips_v231128-f1.ijm"; t = stripKnownExtensionFromString(getTitle()); defPath = getDirectory("image"); if (defPath=="") defPath = getDirectory("file"); iMMagickPath = findAppPath("ImageMagick","magick.exe","not found"); if (File.exists(iMMagickPath)) iMMagick = true; else iMMagick = false; getDimensions(imageWidth, imageHeight, channels, slices, frames); selEType = selectionType; if (selEType>=0) { getSelectionBounds(selEX, selEY, selEWidth, selEHeight); if ((selEWidth + selEHeight)<6) selEType=-1; /* Ignore junk selections that are suspiciously small */ if (selEType==5) getSelectionCoordinates(selLX, selLY); leftLimit = selEX; rightLimit = selEX + selEWidth; topLimit = selEY; bottomLimit = selEY + selEHeight; maxHeight = selEHeight; } else { leftLimit = 0; rightLimit = imageWidth; topLimit = selEY; bottomLimit = imageHeight; maxHeight = imageHeight; } run("Select None"); activeImage = getTitle(); Dialog.create("Strip Parameters: " + macroL); Dialog.addNumber("Left coordinate limit", leftLimit, 0, 10, "pixels"); Dialog.addNumber("Right coordinate limit", rightLimit, 0, 10, "pixels"); Dialog.addNumber("Top coordinate limit", topLimit, 0, 10, "pixels"); Dialog.addNumber("Bottom coordinate limit", bottomLimit, 0, 10, "pixels"); Dialog.addNumber("Block width", imageWidth/10, 0, 10, "pixels"); Dialog.addNumber("Block height", maxHeight, 0, 10, "pixels"); Dialog.addNumber("Overlap", 0, 0, 10, "pixels"); Dialog.addDirectory("Export directory", defPath); Dialog.setInsets(0,68,10); Dialog.addMessage("Export directory above: " + defPath); Dialog.addString("Filename \(prefix, counter and '_lzw.tif' will be added\)", t,30); if (iMMagick) Dialog.addString("ImageMagick settings", " -compress lzw ",30); Dialog.show(); leftLimit = Dialog.getNumber; rightLimit = Dialog.getNumber; topLimit = Dialog.getNumber; bottomLimit = Dialog.getNumber; blockWidth = Dialog.getNumber; blockHeight = Dialog.getNumber; overlap = Dialog.getNumber; newPath = Dialog.getString(); prefix = Dialog.getString(); if (iMMagick) tiffSettings = Dialog.getString(); totalWidth = abs(rightLimit-leftLimit); blockPixelCount = blockWidth * blockHeight; rows = 1; columns = 1; if (blockHeight bottomLimit) blockHeight = bottomLimit-yStart; for(j=0,xStart=leftLimit;jrightLimit) blockWidth = rightLimit-xStart; makeRectangle(xStart, yStart, blockWidth, blockHeight); newName = prefix+"_C"+j+1; if(rows>1) newName += "R" + i+1; run("Duplicate...", newName); saveTIFFPath = newPath + newName + "_lzw.tif"; showStatus("Saving uncompressed TIFF using ImageJ to be used by IM"); saveTIFFPath = pathOverwriteCheck(saveTIFFPath); saveAs("TIFF",saveTIFFPath); fileSaves = Array.concat(fileSaves,saveTIFFPath); xStart += blockWidth - overlap; close(); } if (rows>1) yStart += blockHeight - overlap; } if(selEType==0) makeRectangle(selEX, selEY, selEWidth, selEHeight); else if(selEType==1) makeOval(selEX, selEY, selEWidth, selEHeight); savedFiles = lengthOf(fileSaves); if (savedFiles<1) exit("Fail: no saved files"); delayTime = 500 + blockPixelCount/4E4; wait(delayTime); if (iMMagick) { for (i=0; i0){ protectedPath = substring(string,0,protectedPathEnd); string = substring(string,protectedPathEnd); } unusefulCombos = newArray("-", "_"," "); for (i=0; i=0) string = replace(string,combo,unusefulCombos[i]); } } if (lastIndexOf(string, ".")>0 || lastIndexOf(string, "_lzw")>0) { knownExts = newArray(".avi", ".csv", ".bmp", ".dsx", ".gif", ".jpg", ".jpeg", ".jp2", ".png", ".tif", ".txt", ".xlsx"); knownExts = Array.concat(knownExts,knownExts,"_transp","_lzw"); kEL = knownExts.length; for (i=0; i0){ preChan = substring(string,0,iChanLabels); postChan = substring(string,iChanLabels); while (indexOf(preChan,knownExts[i])>0){ preChan = replace(preChan,knownExts[i],""); string = preChan + postChan; } } } while (endsWith(string,knownExts[i])) string = "" + substring(string, 0, lastIndexOf(string, knownExts[i])); } } unwantedSuffixes = newArray(" ", "_","-"); for (i=0; i0){ if(!endsWith(protectedPath,fS)) protectedPath += fS; string = protectedPath + string; } return string; }