How to create a techniqueIcon from RGB bitmap in GMS 3.x - dm-script
In GMS 3.x we can create "Techniques" with customized techniqueIcon:
Image techniqueIcon := RGBImage( "Test icon", 4, 75, 75 )
techniqueIcon = RGB( icol, irow, iradius )
String techniqueName = "MyTechnique"
Object technique = CreateTechnique( techniqueName, techniqueIcon )
How do I make the "techniqueIcon" to have proper transparency (i.e. alpha channel)? I tried the RGBA() function but the result icon still does not have the transparency as I desired. I can, however, create the icon in *.PNG format from an external application and assign it to a technique via the "Manage Custom Techniques ..." menu command.
I believe there is currently no proper way to set transparency to icons via scripting. DM scripting does not fully support alpha-values in RGB images.
However, your comment was interesting and send me experimenting.
The tag saved at the provided location, when watched in the tag-browser, does not seem to be a typical "array" tag, because those would display as (values not shown). Instead, it shows as a long string.
The size of the tag (as given by TagGroupGetTagSize()) in bytes matches 4 bytes * width * height of the bitmap.
Assuming that each pixel in the icon has a byte-sizes values for R , G , B and Alpha, I'm guessing the "string" is just DigitalMicrograph's way of (mis)representing the binary array data, because it's not a known array form (aka image).
This led me to come up with the following script:
RGBImage GetBitMapFromCustomTechniqueIcon( number index)
{
RGBImage bitmap
taggroup tg
string path = "WorkflowManager:Custom Techniques:["+index+"]"
if ( !GetPersistentTagGroup().TagGroupGetTagAsTagGroup(path,tg) ) Throw("Tagpath not found")
number w,h
if ( !tg.TagGroupGetTagAsLong("Bitmap height",h) ) Throw("Height tag not found")
if ( !tg.TagGroupGetTagAsLong("Bitmap width",w) ) Throw("Width tag not found")
string str
if ( !tg.TagGroupGetTagAsString("Bitmap data",str) ) Throw("Bitmap data tag not found.")
{
number nPts = w*h
if ( str.len()/4 != nPts ) Throw("Data length does not seem to match icon size")
image imgR := IntegerImage("Red",1,0,w,h)
image imgG := IntegerImage("Green",1,0,w,h)
image imgB := IntegerImage("Blue",1,0,w,h)
image imgA := IntegerImage("Alpha",1,0,w,h)
// Browse and convert string
for( number i=0; i<nPts; i++)
{
imgR[i%w,i/w] = asc(str.mid(i*4,1))
imgG[i%w,i/w] = asc(str.mid(i*4+1,1))
imgB[i%w,i/w] = asc(str.mid(i*4+2,1))
imgA[i%w,i/w] = asc(str.mid(i*4+3,1))
}
imgR.FlipVertical()
imgG.FlipVertical()
imgB.FlipVertical()
imgA.FlipVertical()
bitmap := RGBA(imgR,imgG,imgB,imgA)
}
return bitmap
}
With this script, I'm indeed able to read out the stored icon and show it as RGB image.
However, I was less successful when writing the tags. The icons never update correctly, even after a DM restart.
void WriteCustomTechniqueIcon(number index,image imgR,image imgG,image imgB,image imgA)
{
taggroup tg
string path = "WorkflowManager:Custom Techniques:["+index+"]"
if ( !GetPersistentTagGroup().TagGroupGetTagAsTagGroup(path,tg) ) Throw("Tagpath not found")
number w = imgR.ImageGetDimensionSize(0)
number h = imgR.ImageGetDimensionSize(1)
if ( w != imgG.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgG.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
if ( w != imgB.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgB.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
if ( w != imgA.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgA.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
tg.TagGroupSetTagAsLong("Bitmap height",h)
tg.TagGroupSetTagAsLong("Bitmap width",w)
imgR.FlipVertical()
imgG.FlipVertical()
imgB.FlipVertical()
imgA.FlipVertical()
// Create encoded data string
string str = ""
number nPts=w*h
for( number i=0; i<nPts; i++)
{
str+= uncchr(sum(imgR[i%w,i/w]))
str+= uncchr(sum(imgG[i%w,i/w]))
str+= uncchr(sum(imgB[i%w,i/w]))
str+= uncchr(sum(imgA[i%w,i/w]))
}
tg.TagGroupSetTagAsString("Bitmap data",str)
}
Not sure what I'm missing out on here.
EDIT
I'm getting nearly there when I trick the system and write a proper uint8 array of 4 times the image size using the code below:
void WriteCustomTechniqueIcon2(number index,image imgR,image imgG,image imgB,image imgA)
{
taggroup tg
string path = "WorkflowManager:Custom Techniques:["+index+"]"
if ( !GetPersistentTagGroup().TagGroupGetTagAsTagGroup(path,tg) ) Throw("Tagpath not found")
number w = imgR.ImageGetDimensionSize(0)
number h = imgR.ImageGetDimensionSize(1)
if ( w != imgG.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgG.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
if ( w != imgB.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgB.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
if ( w != imgA.ImageGetDimensionSize(0) ) Throw( "Images not of same size" )
if ( h != imgA.ImageGetDimensionSize(1) ) Throw( "Images not of same size" )
tg.TagGroupSetTagAsLong("Bitmap height",h)
tg.TagGroupSetTagAsLong("Bitmap width",w)
imgR.FlipVertical()
imgG.FlipVertical()
imgB.FlipVertical()
imgA.FlipVertical()
image array := IntegerImage("",1,0,w*4,h)
array.slice2(0,0,0, 0,w,4,1,h,1) = imgB
array.slice2(1,0,0, 0,w,4,1,h,1) = imgG
array.slice2(2,0,0, 0,w,4,1,h,1) = imgR
array.slice2(3,0,0, 0,w,4,1,h,1) = imgA
tg.TagGroupSetTagAsArray("Bitmap data",array)
}
The icon loads after DM is restarted.
However, the colors are all dimmed down and I don't really know why. Alpha, however, is working (including mixing values)
Related
How to avoid text overlapping in odoo 10 nvd3 pie chart
I am using odoo 10. When I go to tree view then charts then click on pie chart icon. Pie Labels are overlapping at some points. I tried to some work around in /web/static/nvd3/nv.d3.js file but it is either giving me errors or n effect. Could anyone help me how to achieve this without text overlapping on piechart? /* Overlapping pie labels are not good. What this attempts to do is, prevent overlapping. Each label location is hashed, and if a hash collision occurs, we assume an overlap. Adjust the label's y-position to remove the overlap. */ var center = labelsArc[i].centroid(d); var percent = getSlicePercentage(d); if (d.value && percent >= labelThreshold) { var hashKey = createHashKey(center); if (labelLocationHash[hashKey]) { center[1] -= avgHeight; } labelLocationHash[createHashKey(center)] = true; } return 'translate(' + center + ').rotateLabels(-45)' } Above code giving me all text labels centered in piechart middle/centre overlapped on each other. if I remove .rotateLabels(-45) then labels are outside the pie circle but some text overlapping on each other. Thanks in advance!
This worked for me. Do not apply rotateLabels(-45) as I have applied in the question. set ShowLabels=true and labelSunbeamLayout=true as shown below in nv.d3.js file. //============================================================ // Public Variables with Default Settings //------------------------------------------------------------ var margin = {top: 0, right: 0, bottom: 0, left: 0} , width = 500 , height = 500 , getX = function(d) { return d.x } , getY = function(d) { return d.y } , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one , container = null , color = nv.utils.defaultColor() , valueFormat = d3.format(',.2f') , showLabels = true , labelsOutside = true , labelType = "key" , labelThreshold = .02 //if slice percentage is under this, don't show label , donut = false , title = false , growOnHover = true , titleOffset = 0 , labelSunbeamLayout = true , startAngle = false , padAngle = false , endAngle = false , cornerRadius = 0 , donutRatio = 0.5 , arcsRadius = [] , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd') ;
What is the right way to get term positions in a Lucene document?
The example in this question and some others I've seen on the web use postings method of a TermVector to get terms positions. Copy paste from the example in the linked question: IndexReader ir = obtainIndexReader(); Terms tv = ir.getTermVector( doc, field ); TermsEnum terms = tv.iterator(); PostingsEnum p = null; while( terms.next() != null ) { p = terms.postings( p, PostingsEnum.ALL ); while( p.nextDoc() != PostingsEnum.NO_MORE_DOCS ) { int freq = p.freq(); for( int i = 0; i < freq; i++ ) { int pos = p.nextPosition(); // Always returns -1!!! BytesRef data = p.getPayload(); doStuff( freq, pos, data ); // Fails miserably, of course. } } } This code works for me but what drives me mad is that the Terms type is where the position information is kept. All the documentation I've seen keep saying that term vectors keep position data. However, there are no methods on this type to get that information! Older versions of Lucene apparently had a method but as of at least version 6.5.1 of Lucene, that is not the case. Instead I'm supposed to use postings method and traverse the documents but I already know which document I want to work on! The API documentation does not say anything about postings returning only the current document (the one the term vector belongs to) but when I run it, I only get the current doc. Is this the correct and only way to get position data from term vectors? Why such an unintuitive API? Is there a document that explains why the previous approach changed in favour of this?
Don't know about "right or wrong" but for version 6.6.3 this seems to work. private void run() throws Exception { Directory directory = new RAMDirectory(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); IndexWriter writer = new IndexWriter(directory, indexWriterConfig); Document doc = new Document(); // Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES FieldType type = new FieldType(); type.setStoreTermVectors(true); type.setStoreTermVectorPositions(true); type.setStoreTermVectorOffsets(true); type.setIndexOptions(IndexOptions.DOCS); Field fieldStore = new Field("tags", "foo bar and then some", type); doc.add(fieldStore); writer.addDocument(doc); writer.close(); DirectoryReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); Term t = new Term("tags", "bar"); Query q = new TermQuery(t); TopDocs results = searcher.search(q, 1); for ( ScoreDoc scoreDoc: results.scoreDocs ) { Fields termVs = reader.getTermVectors(scoreDoc.doc); Terms f = termVs.terms("tags"); TermsEnum te = f.iterator(); PostingsEnum docsAndPosEnum = null; BytesRef bytesRef; while ( (bytesRef = te.next()) != null ) { docsAndPosEnum = te.postings(docsAndPosEnum, PostingsEnum.ALL); // for each term (iterator next) in this field (field) // iterate over the docs (should only be one) int nextDoc = docsAndPosEnum.nextDoc(); assert nextDoc != DocIdSetIterator.NO_MORE_DOCS; final int fr = docsAndPosEnum.freq(); final int p = docsAndPosEnum.nextPosition(); final int o = docsAndPosEnum.startOffset(); System.out.println("p="+ p + ", o=" + o + ", l=" + bytesRef.length + ", f=" + fr + ", s=" + bytesRef.utf8ToString()); } } }
PDF article region identification
Which PdfBox APIs can I use for identification of regions where a region is a rectangle encapsulating one article so that I can then extract the text of the article. I am thinking of parsing the PDF content where large white space areas encapsulating text would be identified as regions. Here is a code which extracts one region where the size and placement of the region is hard coded: System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider"); PDDocument pdf = PDDocument.load(new File("SevenPropertiesofHighlySecureDevices.pdf")); PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); // 1 pt is equal to 1/72 inch Rectangle2D rectangle = new Rectangle2D.Double(0,0,200,200); String regionName = "First Article Region"; stripper.addRegion(regionName, rectangle); stripper.extractRegions(pdf.getPage(0)); LOGGER.info("getTextForRegion: \n{}", stripper.getTextForRegion(regionName)); If anyone is tempted to "scan" a PDF to find out where the white space areas are so that this information would be used to determine rectangular regions for the purpose of extraction of articles then I would like to let you know that it is very slow and unlikely usable: System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider"); PDDocument pdf = PDDocument.load(new File("4.pdf")); PDPage page = pdf.getPage(1); PDRectangle cropBox = page.getCropBox(); float docWidth = cropBox.getUpperRightX(); float docHeight = cropBox.getUpperRightY(); float recWidth = 10; float rectHeight = 10; float xStep = recWidth / 2; float yStep = rectHeight / 2; String regionName = "docScannerRegion"; String docLeftMarginIndicator = "|"; String docRightMarginIndicator = "|"; String nonTextArea = " "; String textArea = "-"; PDFTextStripperByArea stripper = new PDFTextStripperByArea(); for (float y = 0; y < docHeight; y = y + yStep) { System.out.print(docLeftMarginIndicator); for(float x = 0; x < docWidth; x = x + xStep) { stripper.addRegion(regionName, new Rectangle2D.Double(x, y, recWidth, rectHeight)); stripper.extractRegions(page); String txt = stripper.getTextForRegion(regionName).trim(); if (StringUtils.isBlank(txt)) { System.out.print(nonTextArea); } else { System.out.print(textArea); } } System.out.println(docRightMarginIndicator); }
how to parse this sample of json
I assumed this as an array and I typed _price1=[venues valueForKey:#"price_total”]; I got this an an output ,I expected to get 6 as the count when i typed this NSLog(#"%lu", (unsigned long)_price1.count); The fact is that I got 1 as the output for the count. ( ( 772, 912, 912, 912, 912, 935 ) ) My doubt is how can I parse it so that the ouput is an array.Do tell me the meaning about ( ) [this brackets] when json is considered. my code is Ok my code is : -(void)initialize{ id venues = [[_json valueForKey:#"hotels_prices"]valueForKey:#"agent_prices”]; _price1=[venues valueForKey:#"price_total"];//global array... NSLog(#"%#", _price1); NSLog(#"%lu", (unsigned long)_price1.count);} json is: this is (id venues ) according to the code ( ( { id = 146; "price_per_room_night" = 158; "price_total" = 944; "room_offers" = ( //problem { available = 0; "meal_plan" = "Room only"; "policy_dto" = { }; rooms = ( { adults = 0; children = 0; type = "Double room"; "type_id" = "ROOMTYPE_DOUBLE"; } );//problem }, //see that brackets (); is the problem
Yep! I got the answer as Matt told the brackets (); should be treated as an array the id venues = [[_json valueForKey:#"hotels_prices"]valueForKey:#"agent_prices”]; gives the above json but if I want to get the meal plan NSArray*meal=[[_json valueForKey:#"room_offers"]valueForKey:#"meal_plan”]; Then I can type: NSArray*meal_plan=[meal objectAtIndex:0];//or any other index not beyond the array limit.. We can get the desired output and the array meal can be parsed normally.
selenium webdriver label automation in sequence verify
[![enter image description here][1]][1] in image the numbering is not in sequence order, I need to verify if no are in sequence 1, 2, 3 , 4 then test case pass else fail. Social Networking URL #1: text box Social Networking URL #2: text box Social Networking URL #1: textbox Social Networking URL #1: text box To verify Numbering should go 1,2,3,4 please help, they are not allowing me to attach image for more clearance
You can get the all elements by driver.find_elements_by_css_selector(css_selector) with the given css_selector then you can get it text by .text finally check if the number is increasing sequencially: Python: def check_if_sequencial(css_selector): result = True elms = driver.find_elements_by_css_selector(css_selector) for i in xrange(len(elms) - 1): text1 = elms[i].text text2 = elms[i+1].text # slicing after dash to end of string, convert to int number1 = int(text1[text1.index("#")+1 : len(text1)]) number2 = int(text1[text2.index("#")+1 : len(text2)]) if number2 != number1 + 1: print "number1: %d and number2: %d are not sequencial" %(number1, number2) result = False break return result Java: public boolean check_if_sequencial(String css_selector){ boolean result = true; List elms = driver.findElements(By.cssSelector(css_selector)); for (int i = 0; i < elms.length - 1; i++) { String text1 = elms[i].text; String text2 = elms[i+1].text; # spliting by dash take the second, convert to int String[] parts1 = text1.split("#"); int number1 = Integer.parseInt(parts1[1]); String[] parts2 = text2.split("#"); int number2 = Integer.parseInt(parts2[1]); if (number2 != number1 + 1){ System.out.println("number1: " + number1 + " and number2: " + number2 + " are not sequencial"); result = false; break; } } return result; }