Package: ImageUtils
ImageUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ImageUtils() |
|
|
|
|
|
||||||||||||||||||||
gaussianBlurKernel(double) |
|
|
|
|
|
||||||||||||||||||||
gaussianBlurKernel(double, int) |
|
|
|
|
|
||||||||||||||||||||
getICCProfile(RenderedImage) |
|
|
|
|
|
||||||||||||||||||||
getICCProfileName(ICC_Profile) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image;
28:
29: import java.awt.color.ICC_ColorSpace;
30: import java.awt.color.ICC_Profile;
31: import java.awt.image.Kernel;
32: import java.awt.image.RenderedImage;
33:
34: /***********************************************************************************************************************
35: *
36: * @author Fabrizio Giudici
37: *
38: **********************************************************************************************************************/
39: public final class ImageUtils
40: {
41: /*******************************************************************************************************************
42: *
43: *
44: ******************************************************************************************************************/
45: public static ICC_Profile getICCProfile (final RenderedImage image)
46: {
47: final var colorSpace = image.getColorModel().getColorSpace();
48:
49:• if (colorSpace instanceof ICC_ColorSpace)
50: {
51: final var iccColorSpace = (ICC_ColorSpace)colorSpace;
52:
53: return iccColorSpace.getProfile();
54: }
55:
56: return null;
57: }
58:
59: /*******************************************************************************************************************
60: *
61: *
62: ******************************************************************************************************************/
63: public static String getICCProfileName (final ICC_Profile profile)
64: {
65:• if (profile == null)
66: {
67: return null;
68: }
69:
70: final var xx = profile.getData(ICC_Profile.icSigProfileDescriptionTag);
71: final var offset = 12;
72: int count;
73:
74:• for (count = 1; xx[offset + count] != 0; count++)
75: {
76: ;
77: }
78:
79: return new String(xx, 0, offset, count);
80: }
81:
82: /*******************************************************************************************************************
83: * Generate a square gaussian blur kernel to be used with the convolve Op
84: *
85: * @param sigma standard deviation of gaussian bell
86: * @param size size (in pixels) of the square filter; if it's an even number,
87: * it is set to the closest greater odd number
88: * @return gaussian blur kernel
89: ******************************************************************************************************************/
90: public static Kernel gaussianBlurKernel (final double sigma, int size)
91: {
92: // Size od kernel must be an odd number
93:• if (size % 2 == 0)
94: {
95: size += 1;
96: }
97:
98: final var matrix = new float[size * size];
99: var sum = 0.0;
100:
101:• for (var x = -size / 2; x <= size / 2; x++)
102: {
103:• for (var y = -size / 2; y <= size / 2; y++)
104: {
105: final double rsq = x * x + y * y;
106: final var gauss = 1 / (Math.sqrt(2 * Math.PI) * sigma) * Math.exp(-rsq / (2 * sigma * sigma));
107: sum += gauss;
108:
109: final var index = size * (y + size / 2) + (x + size / 2);
110: matrix[index] = (float)gauss;
111: }
112: }
113:
114: // Normalize the kernel
115:• for (var i = 0; i < matrix.length; i++)
116: {
117: matrix[i] /= (float)sum;
118:
119: System.out.print(matrix[i]);
120:• if (i % size == 0 && i != 0)
121: {
122: System.out.println();
123: }
124: else
125: {
126: System.out.print(" ");
127: }
128: }
129:
130: return new Kernel(size, size, matrix);
131: }
132:
133: /*******************************************************************************************************************
134: * Generate a square gaussian blur kernel to be used with the JAI
135: * convolve operation. The kernel size is calculated automatically based
136: * on the standard deviation parameter, so as to comprise a +/- 3*sigma
137: * bell around the center
138: *
139: * @param sigma standard deviation of gaussian bell
140: * @return gaussian blur kernel
141: ******************************************************************************************************************/
142: public static Kernel gaussianBlurKernel (final double sigma)
143: {
144: var size = 0;
145:
146:• if (sigma < 0.5)
147: {
148: size = 3;
149: }
150:• else if (sigma >= 0.5 && sigma < (5.0 / 6.0))
151: {
152: size = 5;
153: }
154:• else if (sigma >= (5.0 / 6.0) && sigma < (7.0 / 6.0))
155: {
156: size = 7;
157: }
158:• else if (sigma >= (7.0 / 6.0) && sigma < 1.5)
159: {
160: size = 9;
161: }
162:• else if (sigma >= 1.5 && sigma < (11.0 / 6.0))
163: {
164: size = 11;
165: }
166:• else if (sigma >= (11.0 / 6.0) && sigma < (13.0 / 6.0))
167: {
168: size = 13;
169: }
170:• else if (sigma >= (13.0 / 6.0))
171: {
172: size = 15;
173: }
174:
175: return gaussianBlurKernel(sigma, size);
176: }
177:
178:
179: }