1 /*
2  *
3  * Copyright (C) 2007,2008  Markko Merzin (markko.merzin@ut.ee)
4  *
5  * This file is part of ImageJ plugin Volumest.
6  * 
7  * Volumest plugin is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24import ij.measure.Calibration;
25import ij.process.ImageProcessor;
26import java.util.Iterator;
27
28/**
29 * Methods for drawing grids and markers.
30 * @author Markko Merzin, markko.merzin@ut.ee
31 * @version 1.0
32 */
33public class GraphicsHandler {
34
35    private Calibration calibration;
36
37    /**
38     * @param calibration Has parameters from calibration.
39     */
40    public GraphicsHandler(Calibration calibration) {
41        this.calibration = calibration;
42    }
43
44    /**
45     * Draws grid to Imageprocessor.
46     * @param grid ee.ut.ee.mrz.Grid: grid model.
47     * @param ip ImageProcessor to draw.
48     */
49    public void drawGrid(Grid grid, ImageProcessor ip) {
50        Iterator iter = grid.getGridPoints().iterator();
51        while (iter.hasNext()) {
52            GridPoint point = (GridPoint) iter.next();
53            if (point.isIntersection()) {
54                // Color col = new Color( (int) ij.Prefs.getInt( ".Volumest.intersectionColor", 0 ) );
55                ip.setColor(Settings.getInstance().getIntersectionColor());
56            } else {
57                if (point.isActive()) {
58                    // Color col = new Color( (int) ij.Prefs.getInt( ".Volumest.selectionColor", 0 ) );
59                    ip.setColor(Settings.getInstance().getSelectionColor());
60                } else {
61                    // Color col = new Color( (int) ij.Prefs.getInt( ".Volumest.gridColor", 0 ) );
62                    ip.setColor(Settings.getInstance().getGridColor());
63                }
64            }
65            // grid width in pixels
66            double gwip = Math.round(Settings.getInstance().getGridWidth() / calibration.pixelWidth);
67
68            double vertexLen = Settings.getInstance().getVertexLength() / 100d;
69
70            Reper reper = grid.getReper();
71
72            if (point.isIntersection()) {
73                // wasInter = true;
74
75                int x = (int) Math.round(point.getXInAfineReper());
76                int y = (int) Math.round(point.getYInAfineReper());
77                // if yo change qwip/4 here, change in Grid.activateIntersecionPoint too
78                int yUp = (int) Math.round(point.getYInAfineReper() + gwip / 4);
79                int yDown = (int) Math.round(point.getYInAfineReper() - gwip / 4);
80
81                int xLeft = (int) Math.round(point.getXInAfineReper() - gwip / 4);
82                int xRight = (int) Math.round(point.getXInAfineReper() + gwip / 4);
83
84                // ip.setLineWidth( (int) ij.Prefs.get( ".Volumest.gridLineThickness", 1 ) );
85
86                ip.setLineWidth(Settings.getInstance().getGridLineThickness());
87
88                ip.moveTo(x, yUp);
89                ip.lineTo(x, yDown);
90
91                ip.moveTo(xRight, y);
92                ip.lineTo(xLeft, y);
93
94            } else {
95
96                if ( point.isActive() || !Settings.getInstance().isDontDisplayGrid() ) {
97                    if (Settings.getInstance().getGridType() == Settings.GRID_RIGHT_ANGLES) {
98
99                        GridPoint pointUp = new GridPoint(point.getXInCrossReper(), point.getYInCrossReper() - gwip / 2 * vertexLen, reper);
00                        GridPoint pointRight = new GridPoint(point.getXInCrossReper() + gwip / 2 * vertexLen, point.getYInCrossReper(), reper);
01
02                        int x = (int) Math.round(point.getXInAfineReper());
03                        int y = (int) Math.round(point.getYInAfineReper());
04
05                        int xUp = (int) Math.round(pointUp.getXInAfineReper());
06                        int yUp = (int) Math.round(pointUp.getYInAfineReper());
07
08                        int xRight = (int) Math.round(pointRight.getXInAfineReper());
09                        int yRight = (int) Math.round(pointRight.getYInAfineReper());
10
11                        // ip.setLineWidth( (int) ij.Prefs.get( ".Volumest.gridLineThickness", 1 ) );
12
13                        ip.setLineWidth(Settings.getInstance().getGridLineThickness());
14
15                        ip.moveTo(x, y);
16                        ip.lineTo(xUp, yUp);
17
18                        ip.moveTo(x, y);
19                        ip.lineTo(xRight, yRight);
20
21
22                    }
23
24                    if (Settings.getInstance().getGridType() == Settings.GRID_LINES) {
25
26                        GridPoint pointUp = new GridPoint(point.getXInCrossReper(), point.getYInCrossReper() - gwip / 2, reper);
27                        GridPoint pointRight = new GridPoint(point.getXInCrossReper() + gwip / 2, point.getYInCrossReper(), reper);
28                        GridPoint pointDown = new GridPoint(point.getXInCrossReper(), point.getYInCrossReper() + gwip / 2, reper);
29                        GridPoint pointLeft = new GridPoint(point.getXInCrossReper() - gwip / 2, point.getYInCrossReper(), reper);
30
31                        int x = (int) Math.round(point.getXInAfineReper());
32                        int y = (int) Math.round(point.getYInAfineReper());
33
34                        int xUp = (int) Math.round(pointUp.getXInAfineReper());
35                        int yUp = (int) Math.round(pointUp.getYInAfineReper());
36
37                        int xDown = (int) Math.round(pointDown.getXInAfineReper());
38                        int yDown = (int) Math.round(pointDown.getYInAfineReper());
39
40                        int xLeft = (int) Math.round(pointLeft.getXInAfineReper());
41                        int yLeft = (int) Math.round(pointLeft.getYInAfineReper());
42
43                        int xRight = (int) Math.round(pointRight.getXInAfineReper());
44                        int yRight = (int) Math.round(pointRight.getYInAfineReper());
45
46
47                        // ip.setLineWidth( (int) ij.Prefs.get( ".Volumest.gridLineThickness", 1 ) );
48
49                        ip.setLineWidth(Settings.getInstance().getGridLineThickness());
50
51                        ip.moveTo(x, y);
52                        ip.lineTo(xUp, yUp);
53
54                        ip.moveTo(x, y);
55                        ip.lineTo(xRight, yRight);
56
57                        ip.moveTo(x, y);
58                        ip.lineTo(xDown, yDown);
59
60                        ip.moveTo(x, y);
61                        ip.lineTo(xLeft, yLeft);
62
63                    }
64                }
65            }
66        }
67    }
68
69    /**
70     * Makes copy of image pixels.
71     * @param localPixels Array of pixels to copy.
72     * @return Array of copied pixels.
73     */
74    public Object copyImage(Object localPixels) {
75        Object pixelsCopy = null;
76
77        if (localPixels instanceof int[]) {
78
79            int[] localPixelsCasted = (int[]) localPixels;
80            int[] pixels = new int[localPixelsCasted.length];
81
82            for (int i = 0; i < pixels.length; i++) {
83                pixels[i] = localPixelsCasted[i];
84            }
85
86            pixelsCopy = pixels;
87
88        } else if (localPixels instanceof byte[]) {
89            byte[] localPixelsCasted = (byte[]) localPixels;
90            byte[] pixels = new byte[localPixelsCasted.length];
91
92            for (int i = 0; i < pixels.length; i++) {
93                pixels[i] = localPixelsCasted[i];
94            }
95
96            pixelsCopy = pixels;
97
98        } else if (localPixels instanceof short[]) {
99            short[] localPixelsCasted = (short[]) localPixels;
00            short[] pixels = new short[localPixelsCasted.length];
01
02            for (int i = 0; i < pixels.length; i++) {
03                pixels[i] = localPixelsCasted[i];
04            }
05
06            pixelsCopy = pixels;
07
08        } else if (localPixels instanceof float[]) {
09            float[] localPixelsCasted = (float[]) localPixels;
10            float[] pixels = new float[localPixelsCasted.length];
11
12            for (int i = 0; i < pixels.length; i++) {
13                pixels[i] = localPixelsCasted[i];
14            }
15
16            pixelsCopy = pixels;
17
18        }
19
20        return pixelsCopy;
21    }
22}
23