|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.baremaps.raster; |
| 19 | + |
| 20 | +import java.awt.image.BufferedImage; |
| 21 | + |
| 22 | +/** |
| 23 | + * Provides utility methods for processing raster images, particularly for elevation data. |
| 24 | + */ |
| 25 | +public class ElevationUtils { |
| 26 | + |
| 27 | + private static final double ELEVATION_SCALE = 256.0 * 256.0; |
| 28 | + private static final double ELEVATION_OFFSET = 10000.0; |
| 29 | + |
| 30 | + private ElevationUtils() { |
| 31 | + // Private constructor to prevent instantiation |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Converts a BufferedImage to a grid of elevation values. |
| 36 | + * |
| 37 | + * @param image The input BufferedImage |
| 38 | + * @return A double array representing the elevation grid |
| 39 | + */ |
| 40 | + public static double[] imageToGrid(BufferedImage image) { |
| 41 | + validateImage(image); |
| 42 | + int width = image.getWidth(); |
| 43 | + int height = image.getHeight(); |
| 44 | + double[] grid = new double[width * height]; |
| 45 | + |
| 46 | + for (int y = 0; y < height; y++) { |
| 47 | + for (int x = 0; x < width; x++) { |
| 48 | + grid[y * width + x] = pixelToElevation(image.getRGB(x, y)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return grid; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Converts a grid of elevation values to a BufferedImage. |
| 57 | + * |
| 58 | + * @param grid The input elevation grid |
| 59 | + * @param width The width of the grid |
| 60 | + * @param height The height of the grid |
| 61 | + * @return A BufferedImage representing the elevation data |
| 62 | + */ |
| 63 | + public static BufferedImage gridToImage(double[] grid, int width, int height) { |
| 64 | + validateGrid(grid, width, height); |
| 65 | + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| 66 | + |
| 67 | + for (int y = 0; y < height; y++) { |
| 68 | + for (int x = 0; x < width; x++) { |
| 69 | + image.setRGB(x, y, elevationToPixel(grid[y * width + x])); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return image; |
| 74 | + } |
| 75 | + |
| 76 | + private static double pixelToElevation(int rgb) { |
| 77 | + int r = (rgb >> 16) & 0xFF; |
| 78 | + int g = (rgb >> 8) & 0xFF; |
| 79 | + int b = rgb & 0xFF; |
| 80 | + return (r * ELEVATION_SCALE + g * 256.0 + b) / 10.0 - ELEVATION_OFFSET; |
| 81 | + } |
| 82 | + |
| 83 | + private static int elevationToPixel(double elevation) { |
| 84 | + int value = (int) ((elevation + ELEVATION_OFFSET) * 10.0); |
| 85 | + int r = (value >> 16) & 0xFF; |
| 86 | + int g = (value >> 8) & 0xFF; |
| 87 | + int b = value & 0xFF; |
| 88 | + return (r << 16) | (g << 8) | b; |
| 89 | + } |
| 90 | + |
| 91 | + private static void validateImage(BufferedImage image) { |
| 92 | + if (image == null) { |
| 93 | + throw new IllegalArgumentException("Input image cannot be null"); |
| 94 | + } |
| 95 | + if (image.getWidth() <= 0 || image.getHeight() <= 0) { |
| 96 | + throw new IllegalArgumentException("Image dimensions must be positive"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void validateGrid(double[] grid, int width, int height) { |
| 101 | + if (grid == null || grid.length == 0) { |
| 102 | + throw new IllegalArgumentException("Grid array cannot be null or empty"); |
| 103 | + } |
| 104 | + if (width <= 0 || height <= 0) { |
| 105 | + throw new IllegalArgumentException("Width and height must be positive"); |
| 106 | + } |
| 107 | + if (grid.length != width * height) { |
| 108 | + throw new IllegalArgumentException("Grid array length does not match width * height"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments