|
1 | 1 | import { saveAs } from 'file-saver'; |
2 | 2 | import { toast } from 'react-toastify'; |
| 3 | +import extractChunks from 'png-chunks-extract'; |
| 4 | +import encodeChunks from 'png-chunks-encode'; |
| 5 | +import textChunk from 'png-chunk-text'; |
3 | 6 | import localStorageManager from '../local-storage-manager'; |
4 | 7 | import graphmlBuilder from '../graphml/builder'; |
5 | 8 | import BendingDistanceWeight from '../calculations/bending-dist-weight'; |
@@ -30,8 +33,64 @@ class GraphLoadSave extends GraphUndoRedo { |
30 | 33 | downloadImg(format) { |
31 | 34 | this.cy.emit('hide-bend'); |
32 | 35 | this.cy.$('.eh-handle').remove(); |
33 | | - if (format === 'PNG') saveAs(this.cy.png({ full: true }), `${this.getName()}-DHGWorkflow.png`); |
34 | | - if (format === 'JPG') saveAs(this.cy.jpg({ full: true }), `${this.getName()}-DHGWorkflow.jpg`); |
| 36 | + if (format === 'JPG') { |
| 37 | + saveAs(this.cy.jpg({ full: true }), `${this.getName()}-DHGWorkflow.jpg`); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (format === 'PNG') { |
| 41 | + saveAs(this.cy.png({ full: true }), `${this.getName()}-DHGWorkflow.png`); |
| 42 | + return; |
| 43 | + } |
| 44 | + if (format === 'PNG-EMBEDDED') { |
| 45 | + const b64Uri = this.cy.png({ full: true }); |
| 46 | + const binaryString = window.atob(b64Uri.split(',')[1]); |
| 47 | + const buffer = new Uint8Array(binaryString.length); |
| 48 | + for (let i = 0; i < binaryString.length; i += 1) { |
| 49 | + buffer[i] = binaryString.charCodeAt(i); |
| 50 | + } |
| 51 | + const chunks = extractChunks(buffer); |
| 52 | + chunks.splice(-1, 0, textChunk.encode('graphml', this.getGraphML())); |
| 53 | + const newBuffer = new Uint8Array(encodeChunks(chunks)); |
| 54 | + const blob = new Blob([newBuffer], { type: 'image/png' }); |
| 55 | + saveAs(blob, `${this.getName()}.graphml.png`); |
| 56 | + return; |
| 57 | + } |
| 58 | + if (format === 'JPG-EMBEDDED') { |
| 59 | + const b64Uri = this.cy.jpg({ full: true }); |
| 60 | + const binaryString = window.atob(b64Uri.split(',')[1]); |
| 61 | + const buffer = new Uint8Array(binaryString.length); |
| 62 | + for (let i = 0; i < binaryString.length; i += 1) { |
| 63 | + buffer[i] = binaryString.charCodeAt(i); |
| 64 | + } |
| 65 | + const graphMLStr = this.getGraphML(); |
| 66 | + const graphMLBytes = new TextEncoder().encode(graphMLStr); |
| 67 | + |
| 68 | + const comSegments = []; |
| 69 | + for (let i = 0; i < graphMLBytes.length; i += 65533) { |
| 70 | + const chunk = graphMLBytes.slice(i, i + 65533); |
| 71 | + const segmentLen = chunk.length + 2; |
| 72 | + const seg = new Uint8Array(4 + chunk.length); |
| 73 | + seg[0] = 0xFF; |
| 74 | + seg[1] = 0xFE; |
| 75 | + seg[2] = Math.floor(segmentLen / 256); |
| 76 | + seg[3] = segmentLen % 256; |
| 77 | + seg.set(chunk, 4); |
| 78 | + comSegments.push(seg); |
| 79 | + } |
| 80 | + |
| 81 | + const totalComSize = comSegments.reduce((sum, seg) => sum + seg.length, 0); |
| 82 | + const newBuffer = new Uint8Array(buffer.length + totalComSize); |
| 83 | + newBuffer.set(buffer.slice(0, 2), 0); // FF D8 |
| 84 | + let offset = 2; |
| 85 | + comSegments.forEach((seg) => { |
| 86 | + newBuffer.set(seg, offset); |
| 87 | + offset += seg.length; |
| 88 | + }); |
| 89 | + newBuffer.set(buffer.slice(2), offset); |
| 90 | + |
| 91 | + const blob = new Blob([newBuffer], { type: 'image/jpeg' }); |
| 92 | + saveAs(blob, `${this.getName()}.graphml.jpg`); |
| 93 | + } |
35 | 94 | } |
36 | 95 |
|
37 | 96 | shouldNodeBeSaved(nodeID) { |
|
0 commit comments