PageRenderTime 97ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/protas.roman/src/protas/roman/BCG.java

https://gitlab.com/rprotas/protas.roman
Java | 273 lines | 259 code | 14 blank | 0 comment | 11 complexity | 73cf0b78b0de9dbc1da7839d138fe066 MD5 | raw file
  1. package protas.roman;
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.DataBufferInt;
  4. import javax.swing.JSlider;
  5. public class BCG extends javax.swing.JFrame {
  6. private static final long serialVersionUID = 1L;
  7. Main parent = null;
  8. public BCG(Main parent) {
  9. this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  10. this.parent = parent;
  11. initComponents();
  12. }
  13. void processImage(BufferedImage src, BufferedImage dst, int db) {
  14. DataBufferInt sbuff = (DataBufferInt) src.getRaster().getDataBuffer();
  15. DataBufferInt dbuff = (DataBufferInt) dst.getRaster().getDataBuffer();
  16. int[] sp = sbuff.getData();
  17. int[] dp = dbuff.getData();
  18. int i = 0;
  19. for (int y = 0; y < src.getHeight(); ++y) {
  20. for (int x = 0; x < src.getWidth(); ++x) {
  21. dp[i] = ImageAlgorithms.changeBrightness(sp[i], db);
  22. i++;
  23. }
  24. }
  25. }
  26. public static int [] gammaTable = new int [256];
  27. void processImageGam(BufferedImage src, BufferedImage dst, double db) {
  28. DataBufferInt sbuff = (DataBufferInt) src.getRaster().getDataBuffer();
  29. DataBufferInt dbuff = (DataBufferInt) dst.getRaster().getDataBuffer();
  30. int[] sp = sbuff.getData();
  31. int[] dp = dbuff.getData();
  32. for (int i = 0; i < 256; i++) {
  33. gammaTable[i] = (int)(Math.pow(i / 255.0, db) * 255 + 0.5);
  34. }
  35. int i = 0;
  36. for (int y = 0; y < src.getHeight(); ++y) {
  37. for (int x = 0; x < src.getWidth(); ++x) {
  38. dp[i] = ImageAlgorithms.changeGamma(sp[i]);
  39. i++;
  40. }
  41. }
  42. }
  43. public static int [] contrastTable = new int [256];
  44. public int contrast (int wartosc, byte gama) {
  45. if (gama<0){
  46. wartosc = (int)((127.0 + gama)/127.0 * wartosc - gama); //zmniejszenia kontrastu
  47. }
  48. else {
  49. wartosc = (int)(127/(127.0 - gama) * (wartosc - gama));
  50. }
  51. return wartosc ;
  52. }
  53. void processImageContrast(BufferedImage src, BufferedImage dst, byte dc) {
  54. DataBufferInt sbuff = (DataBufferInt) src.getRaster().getDataBuffer();
  55. DataBufferInt dbuff = (DataBufferInt) dst.getRaster().getDataBuffer();
  56. int[] sp = sbuff.getData();
  57. int[] dp = dbuff.getData();
  58. for (int i = 0; i < 256; i++) {
  59. contrastTable[i] = contrast(i,dc);
  60. }
  61. int i = 0;
  62. for (int y = 0; y < src.getHeight(); ++y) {
  63. for (int x = 0; x < src.getWidth(); ++x) {
  64. dp[i] = ImageAlgorithms.changeContrast(sp[i]);
  65. i++;
  66. }
  67. }
  68. }
  69. private void initComponents() {
  70. brightnessSlider = new javax.swing.JSlider();
  71. vSpiner = new javax.swing.JSpinner();
  72. cancelButton = new javax.swing.JButton();
  73. okButton = new javax.swing.JButton();
  74. GammaSlider = new javax.swing.JSlider();
  75. ContrastSlider = new javax.swing.JSlider();
  76. GamaSpiner = new javax.swing.JSpinner();
  77. ContrastSpiner = new javax.swing.JSpinner();
  78. jLabel1 = new javax.swing.JLabel();
  79. jLabel2 = new javax.swing.JLabel();
  80. jLabel3 = new javax.swing.JLabel();
  81. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  82. setTitle("Color Equalizer");
  83. setMinimumSize(new java.awt.Dimension(500, 250));
  84. setPreferredSize(new java.awt.Dimension(480, 100));
  85. brightnessSlider.setMaximum(255);
  86. brightnessSlider.setMinimum(-255);
  87. brightnessSlider.setValue(0);
  88. brightnessSlider.addChangeListener(new javax.swing.event.ChangeListener() {
  89. public void stateChanged(javax.swing.event.ChangeEvent evt) {
  90. brightnessSliderStateChanged(evt);
  91. }
  92. });
  93. cancelButton.setText("Anuluj");
  94. cancelButton.setPreferredSize(new java.awt.Dimension(125, 25));
  95. cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
  96. public void mouseClicked(java.awt.event.MouseEvent evt) {
  97. cancelButtonMouseClicked(evt);
  98. }
  99. });
  100. okButton.setText("Ok");
  101. okButton.setPreferredSize(new java.awt.Dimension(125, 25));
  102. okButton.addMouseListener(new java.awt.event.MouseAdapter() {
  103. public void mouseClicked(java.awt.event.MouseEvent evt) {
  104. okButtonMouseClicked(evt);
  105. }
  106. });
  107. GammaSlider.setMaximum(200);
  108. GammaSlider.setMinorTickSpacing(10);
  109. GammaSlider.setSnapToTicks(true);
  110. GammaSlider.setValue(100);
  111. GammaSlider.addChangeListener(new javax.swing.event.ChangeListener() {
  112. public void stateChanged(javax.swing.event.ChangeEvent evt) {
  113. GammaSliderStateChanged(evt);
  114. }
  115. });
  116. ContrastSlider.setMaximum(127);
  117. ContrastSlider.setMinimum(-127);
  118. ContrastSlider.setSnapToTicks(true);
  119. ContrastSlider.setToolTipText("");
  120. ContrastSlider.setValue(0);
  121. ContrastSlider.addChangeListener(new javax.swing.event.ChangeListener() {
  122. public void stateChanged(javax.swing.event.ChangeEvent evt) {
  123. ContrastSliderStateChanged(evt);
  124. }
  125. });
  126. jLabel1.setText("Brightness...");
  127. jLabel2.setText("Contrast...");
  128. jLabel3.setText("Gamma...");
  129. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  130. getContentPane().setLayout(layout);
  131. layout.setHorizontalGroup(
  132. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133. .addGroup(layout.createSequentialGroup()
  134. .addContainerGap()
  135. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  137. .addComponent(brightnessSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
  138. .addGap(18, 18, 18)
  139. .addComponent(vSpiner, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
  140. .addGroup(layout.createSequentialGroup()
  141. .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  142. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  143. .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  144. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  145. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  146. .addComponent(ContrastSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  147. .addComponent(GammaSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  148. .addGap(18, 18, 18)
  149. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150. .addComponent(GamaSpiner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
  151. .addComponent(ContrastSpiner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
  152. .addGroup(layout.createSequentialGroup()
  153. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  154. .addComponent(jLabel1)
  155. .addComponent(jLabel2)
  156. .addComponent(jLabel3))
  157. .addGap(0, 0, Short.MAX_VALUE)))
  158. .addContainerGap())
  159. );
  160. layout.setVerticalGroup(
  161. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  162. .addGroup(layout.createSequentialGroup()
  163. .addGap(5, 5, 5)
  164. .addComponent(jLabel1)
  165. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  166. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  167. .addComponent(brightnessSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  168. .addComponent(vSpiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  169. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  170. .addComponent(jLabel2)
  171. .addGap(3, 3, 3)
  172. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  173. .addComponent(ContrastSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  174. .addComponent(ContrastSpiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  175. .addGap(16, 16, 16)
  176. .addComponent(jLabel3)
  177. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  178. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179. .addComponent(GamaSpiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  180. .addComponent(GammaSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  181. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
  182. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  183. .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  184. .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  185. .addGap(26, 26, 26))
  186. );
  187. pack();
  188. }
  189. private void brightnessSliderStateChanged(javax.swing.event.ChangeEvent evt) {
  190. JSlider slider = (JSlider) evt.getSource();
  191. vSpiner.setValue(slider.getValue());
  192. try
  193. {
  194. processImage(parent.originalImage, parent.workImage, slider.getValue());
  195. parent.repaint();
  196. }
  197. catch (Exception e)
  198. {
  199. System.out.println("brightness error: " + e.getMessage());
  200. }
  201. }
  202. private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
  203. parent.revertImage();
  204. setVisible(false);
  205. dispose();
  206. }
  207. private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {
  208. parent.updateImage();
  209. setVisible(false);
  210. dispose();
  211. }
  212. double roundResult (double d, int precise) {
  213. precise = 10^precise;
  214. d = d*precise;
  215. int i = (int) Math.round(d);
  216. return (double) i/precise;
  217. }
  218. private void GammaSliderStateChanged(javax.swing.event.ChangeEvent evt) {
  219. JSlider slider = (JSlider) evt.getSource();
  220. if (slider.getValue()<100) {
  221. GamaSpiner.setValue(slider.getValue()/100.0);
  222. }else {
  223. GamaSpiner.setValue((((slider.getValue()-100)/10.0)));
  224. }
  225. try
  226. {
  227. processImageGam(parent.originalImage, parent.workImage, (GammaSlider.getValue()/100.0) );
  228. parent.repaint();
  229. }
  230. catch (Exception e)
  231. {
  232. System.out.println("gama error: " + e.getMessage());
  233. }
  234. }
  235. private void ContrastSliderStateChanged(javax.swing.event.ChangeEvent evt) {
  236. JSlider slider = (JSlider) evt.getSource();
  237. ContrastSpiner.setValue(slider.getValue());
  238. try
  239. {
  240. processImageContrast(parent.originalImage, parent.workImage, (byte)slider.getValue());
  241. parent.repaint();
  242. }
  243. catch (Exception e)
  244. {
  245. System.out.println("brightness error: " + e.getMessage());
  246. }
  247. }
  248. private javax.swing.JSlider ContrastSlider;
  249. private javax.swing.JSpinner ContrastSpiner;
  250. private javax.swing.JSpinner GamaSpiner;
  251. private javax.swing.JSlider GammaSlider;
  252. private javax.swing.JSlider brightnessSlider;
  253. private javax.swing.JButton cancelButton;
  254. private javax.swing.JLabel jLabel1;
  255. private javax.swing.JLabel jLabel2;
  256. private javax.swing.JLabel jLabel3;
  257. private javax.swing.JButton okButton;
  258. private javax.swing.JSpinner vSpiner;
  259. }