| 26 | |
| 27 | |
| 28 | = Design = |
| 29 | The first two implementation ideas will be implemented, the third one did not prove effective and will not. |
| 30 | |
| 31 | '''ImmImage should be made really immutable and should not expose representation''', so that we can cache some things in it. [[BR]] |
| 32 | The constructor ImmImage(BufferedImage image) should make a deep copy of the argument. Thus if the caller keeps a reference to the buffered image and makes some changes to it, they will not affect the ImmImage. [[BR]] |
| 33 | The same applies for the method BufferedImage toBufferedImage() and its result. [[BR]] |
| 34 | The other constructors will also make a copy of the image in order to be sure that the image is in the preferred format.[[BR]] |
| 35 | A new private method will be created and the constructors and toBufferedImage() will use it: |
| 36 | {{{ |
| 37 | /** |
| 38 | * Deep copy of BufferedImage |
| 39 | * |
| 40 | * @param source |
| 41 | * the source image |
| 42 | * @return |
| 43 | * a copy of the source image converted to out preferred type |
| 44 | */ |
| 45 | private static BufferedImage copyImage(BufferedImage source) { |
| 46 | BufferedImage newImage = new BufferedImage(source.getWidth(), source |
| 47 | .getHeight(), ImmImage.PREFERRED_BI_TYPE); |
| 48 | Graphics2D gt = newImage.createGraphics(); |
| 49 | gt.drawImage(source, 0, 0, null); |
| 50 | gt.dispose(); |
| 51 | return newImage; |
| 52 | } |
| 53 | }}} |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | '''Some helper methods will be added to minimize the need to convert to BufferedImage:''' |
| 59 | * a method drawing the image into graphics. |
| 60 | The ImageElementHelper will not draw the image itself, but will call this method. It is necessary to do so, because ImmImage has direct access to the wrapped BufferedImage, while the element helper would need to copy it. |
| 61 | {{{ |
| 62 | /** |
| 63 | * Draws the image. |
| 64 | * @param g2d |
| 65 | * The graphics where the image will be drawn. |
| 66 | * |
| 67 | */ |
| 68 | public void paint(Graphics2D g2d) { |
| 69 | // implementation |
| 70 | } |
| 71 | }}} |
| 72 | * a method drawing the image multiplied by a given color |
| 73 | |
| 74 | {{{ |
| 75 | * Multiplies all image pixels by given color and draws the resulting image. |
| 76 | * @param g2d |
| 77 | * The graphics where the image will be drawn. |
| 78 | * @param color |
| 79 | * The color which by which to multiply. |
| 80 | * Usually this will be the background color or the color of elements |
| 81 | * above which the image is drawn. |
| 82 | */ |
| 83 | public void paint(Graphics2D g2d, ImmColor color) { |
| 84 | // implementation |
| 85 | } |
| 86 | }}} |
| 87 | |
| 88 | '''Caching ImmImages multiplied by recently used colours''' will make drawing of images above other elements (including background) faster |
| 89 | * A new private field will be added in ImmImage to store the image multiplied by recently used colours - HashMap<ImmColor, ImmImage> colorImgCache. |
| 90 | * A new method for multiplying the image by given color will be added in ImmImage. It will check if the image multiplied by the given color is already cached. If it is it will return the cached value. If not - it will compute it and put it in the cache. |