0
|
1 /*
|
|
2 * Represents a file being edited (rotated)
|
|
3 */
|
|
4 package name.blackcap.imageprep
|
|
5
|
1
|
6 import java.awt.Dimension
|
|
7 import java.awt.Graphics
|
0
|
8 import java.awt.Graphics2D
|
|
9 import java.awt.RenderingHints
|
1
|
10 import java.awt.event.ActionListener
|
0
|
11 import java.awt.image.BufferedImage
|
|
12 import java.io.File
|
|
13 import java.io.IOException
|
|
14 import javax.imageio.ImageIO
|
|
15 import javax.swing.*
|
|
16 import kotlin.math.*
|
|
17
|
|
18 class RotateDialog(val file: File, initialImage: BufferedImage) : JDialog(Application.mainFrame) {
|
|
19 private val BW = 9
|
|
20 private val BW2 = BW * 2
|
|
21 private val MINWIDTH = 512
|
|
22 private val MINHEIGHT = 384
|
|
23
|
1
|
24 private class DrawingPane(initialImage: BufferedImage) : JPanel() {
|
0
|
25 var image: BufferedImage = initialImage
|
|
26 set(value) {
|
|
27 field = value
|
|
28 revalidate()
|
|
29 repaint()
|
|
30 }
|
|
31
|
1
|
32 override fun getPreferredSize() = Dimension(image.width, image.height)
|
0
|
33
|
1
|
34 override protected fun paintComponent(g: Graphics): Unit {
|
0
|
35 g.drawImage(image, 0, 0, null)
|
|
36 }
|
|
37 }
|
1
|
38 private val drawingPane = DrawingPane(initialImage)
|
0
|
39
|
|
40 val image: BufferedImage
|
|
41 get() { return drawingPane.image }
|
|
42
|
1
|
43 private val r90cw = JButton("90° CW").also {
|
0
|
44 it.addActionListener(ActionListener { doRotate(90) })
|
|
45 }
|
|
46
|
1
|
47 private val r180 = JButton("180°").also {
|
0
|
48 it.addActionListener(ActionListener { doRotate(180) })
|
|
49 }
|
|
50
|
1
|
51 private val r90ccw = JButton("90° CCW").also {
|
0
|
52 it.addActionListener(ActionListener { doRotate(270) })
|
|
53 }
|
|
54
|
|
55 /* Theoretically, this should do the rotation in a background thread.
|
|
56 Practically, that is fraught with difficulties, as it involves
|
|
57 manipulating data used by Swing itself. Since the size of the images
|
|
58 being rotated are small, we do it in the foreground. */
|
|
59 private fun doRotate(deg: Int) {
|
|
60 // https://stackoverflow.com/questions/15927014/rotating-an-image-90-degrees-in-java
|
|
61 val rad = java.lang.Math.toRadians(deg.toDouble())
|
|
62 val sinx = sin(rad)
|
|
63 val cosx = cos(rad)
|
|
64 val w = floor(image.width * cosx + image.height * sinx).toInt()
|
|
65 val h = floor(image.height * cosx + image.width * sinx).toInt()
|
|
66 val rotatedImage = BufferedImage(w, h, image.type)
|
|
67 rotatedImage.createGraphics().run {
|
|
68 translate((w - image.width) / 2, (h - image.height) / 2)
|
1
|
69 rotate(rad, image.width.toDouble()/2.0, image.height.toDouble()/2.0)
|
0
|
70 drawRenderedImage(image, null)
|
|
71 dispose()
|
|
72 }
|
|
73 drawingPane.image = rotatedImage
|
|
74 }
|
|
75
|
|
76 init {
|
|
77 defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE
|
|
78 title = "Untitled"
|
|
79 contentPane.apply {
|
|
80 layout = BoxLayout(this, BoxLayout.Y_AXIS)
|
1
|
81 add(JScrollPane(drawingPane).apply {
|
0
|
82 alignmentX = JScrollPane.CENTER_ALIGNMENT
|
|
83 addBorder(BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2))
|
|
84 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
|
|
85 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
|
|
86 background = Application.mainFrame.background
|
|
87 })
|
|
88 add(Box(BoxLayout.X_AXIS).apply {
|
|
89 alignmentX = Box.CENTER_ALIGNMENT
|
|
90 addBorder(BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2))
|
|
91 add(JLabel("Rotate:"))
|
|
92 add(Box.createHorizontalGlue())
|
|
93 add(r90cw)
|
|
94 add(Box.createHorizontalGlue())
|
|
95 add(r180)
|
|
96 add(Box.createHorizontalGlue())
|
|
97 add(r90ccw)
|
|
98 })
|
|
99 }
|
|
100 pack()
|
|
101 }
|
|
102
|
|
103 companion object Factory {
|
|
104 /**
|
|
105 * Make a dialog asynchronously.
|
|
106 *
|
|
107 * @param input java.io.File to read for image.
|
|
108 */
|
|
109 fun makeDialog(input: File): Unit {
|
|
110 Application.mainFrame.useWaitCursor()
|
1
|
111 swingWorker<Pair<BufferedImage?, IOException?>> {
|
0
|
112 inBackground {
|
|
113 try {
|
|
114 val imageIn = ImageIO.read(input) /* IOException */
|
|
115 val ratio = Settings.maxDimension.toDouble() / max(imageIn.width, imageIn.height).toDouble()
|
|
116 if (ratio >= 1.0) {
|
1
|
117 Pair(null, null)
|
0
|
118 } else {
|
|
119 val nWidth = (imageIn.width * ratio).toInt()
|
|
120 val nHeight = (imageIn.height * ratio).toInt()
|
|
121 val imageOut = BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB)
|
|
122 val graphics = imageOut.createGraphics().apply {
|
|
123 setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
|
|
124 setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
|
|
125 setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
|
|
126 }
|
|
127 graphics.drawImage(imageIn, 0, 0, nWidth, nHeight, null)
|
|
128 Pair(imageOut, null)
|
|
129 }
|
|
130 } catch (e: IOException) {
|
|
131 Pair(null, e)
|
|
132 }
|
|
133 }
|
|
134 whenDone {
|
|
135 Application.mainFrame.useNormalCursor()
|
|
136 val (image, error) = get()
|
|
137 if (error != null)
|
|
138 ioExceptionDialog(Application.mainFrame, input, "read", error)
|
1
|
139 else if (image != null)
|
0
|
140 RotateDialog(input, image).title = input.getName()
|
1
|
141 else
|
|
142 JOptionPane.showMessageDialog(Application.mainFrame,
|
|
143 "Image is too small to be scaled.",
|
|
144 "Warning", JOptionPane.WARNING_MESSAGE)
|
0
|
145 }
|
|
146 }
|
|
147 }
|
|
148 }
|
|
149 }
|