|
| 1 | +/* |
| 2 | + *------------------------------------------------------------------------------ |
| 3 | + * Copyright (C) 2024 University of Dundee. All rights reserved. |
| 4 | + * |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License along |
| 16 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + *------------------------------------------------------------------------------ |
| 20 | + */ |
| 21 | +package omero.gateway.facility; |
| 22 | + |
| 23 | +import omero.api.RenderingEnginePrx; |
| 24 | +import omero.gateway.Gateway; |
| 25 | +import omero.gateway.SecurityContext; |
| 26 | +import omero.gateway.exception.DSAccessException; |
| 27 | +import omero.gateway.exception.DSOutOfServiceException; |
| 28 | +import omero.gateway.model.ImageData; |
| 29 | +import omero.romio.PlaneDef; |
| 30 | + |
| 31 | +import java.awt.image.BufferedImage; |
| 32 | + |
| 33 | +public class RenderFacility extends Facility { |
| 34 | + /** |
| 35 | + * Creates a new instance |
| 36 | + * |
| 37 | + * @param gateway Reference to the {@link Gateway} |
| 38 | + */ |
| 39 | + RenderFacility(Gateway gateway) { |
| 40 | + super(gateway); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get a RenderingEngine for an image. |
| 45 | + * |
| 46 | + * @param ctx The security context. |
| 47 | + * @param imageId The image ID |
| 48 | + * @param initDefaultSettings Flag to create default rendering settings if the |
| 49 | + * image doesn't have any for the current user. |
| 50 | + * @return A RenderingEngine for the image |
| 51 | + * @throws DSOutOfServiceException If the connection is broken, or not logged in |
| 52 | + * @throws DSAccessException If an error occurred while trying to retrieve data from OMERO |
| 53 | + * service. |
| 54 | + */ |
| 55 | + public RenderingEnginePrx getRenderingEngine(SecurityContext ctx, long imageId, boolean initDefaultSettings) |
| 56 | + throws DSOutOfServiceException, DSAccessException { |
| 57 | + try { |
| 58 | + long pixelsId = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId).getDefaultPixels().getId(); |
| 59 | + RenderingEnginePrx re = gateway.getRenderingService(ctx, pixelsId); |
| 60 | + if (!re.lookupRenderingDef(pixelsId)) { |
| 61 | + if (initDefaultSettings) { |
| 62 | + re.resetDefaultSettings(true); |
| 63 | + re.lookupRenderingDef(pixelsId); |
| 64 | + } else { |
| 65 | + throw new DSOutOfServiceException("Image doesn't have rendering settings."); |
| 66 | + } |
| 67 | + } |
| 68 | + re.load(); |
| 69 | + return re; |
| 70 | + } catch (Throwable t) { |
| 71 | + handleException(this, t, "Could not load RenderingEngine."); |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks if an image is an RGB(A) image. |
| 78 | + * |
| 79 | + * @param ctx The security context. |
| 80 | + * @param imageId The image ID |
| 81 | + * @return True if the image is RGB(A) |
| 82 | + * @throws DSOutOfServiceException If the connection is broken, or not logged in |
| 83 | + * @throws DSAccessException If an error occurred while trying to retrieve data from OMERO |
| 84 | + * service. |
| 85 | + */ |
| 86 | + public boolean isRGB(SecurityContext ctx, long imageId) throws DSOutOfServiceException, DSAccessException { |
| 87 | + try { |
| 88 | + ImageData img = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId); |
| 89 | + int nChannles = img.getDefaultPixels().getSizeC(); |
| 90 | + if (nChannles < 3 || nChannles > 4) |
| 91 | + return false; |
| 92 | + boolean r = false, g = false, b = false; |
| 93 | + RenderingEnginePrx re = getRenderingEngine(ctx, imageId, true); |
| 94 | + for (int i=0; i<nChannles; i++) { |
| 95 | + int[] ch = re.getRGBA(i); |
| 96 | + if (!r && ch[0] == 255 && ch[1] == 0 && ch[2] == 0) |
| 97 | + r = true; |
| 98 | + if (!g && ch[1] == 255 && ch[0] == 0 && ch[2] == 0) |
| 99 | + g = true; |
| 100 | + if (!b && ch[2] == 255 && ch[0] == 0 && ch[1] == 0) |
| 101 | + b = true; |
| 102 | + } |
| 103 | + return r && g && b; |
| 104 | + } catch (Throwable t) { |
| 105 | + handleException(this, t, "Could not check image."); |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Renders the selected z, t plane of the given image as RGB image. |
| 112 | + * @param ctx The security context. |
| 113 | + * @param imageId The image ID |
| 114 | + * @param z The z plane |
| 115 | + * @param t The time point |
| 116 | + * @return An RGB image ready to be displayed on screen. |
| 117 | + * @throws DSOutOfServiceException If the connection is broken, or not logged in |
| 118 | + * @throws DSAccessException If an error occurred while trying to retrieve data from OMERO |
| 119 | + * service. |
| 120 | + */ |
| 121 | + public int[] renderPlane(SecurityContext ctx, long imageId, int z, int t) |
| 122 | + throws DSOutOfServiceException, DSAccessException { |
| 123 | + try { |
| 124 | + RenderingEnginePrx re = getRenderingEngine(ctx, imageId, true); |
| 125 | + PlaneDef plane = new PlaneDef(omeis.providers.re.data.PlaneDef.XY, 0, |
| 126 | + 0, z, t, null, -1); |
| 127 | + return re.renderAsPackedInt(plane); |
| 128 | + } catch (Throwable e) { |
| 129 | + handleException(this, e, "Could not render plane."); |
| 130 | + } |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Renders the selected z, t plane of the given image as BufferedImage. |
| 136 | + * @param ctx The security context. |
| 137 | + * @param imageId The image ID |
| 138 | + * @param z The z plane |
| 139 | + * @param t The time point |
| 140 | + * @return A BufferedImage ready to be displayed on screen. |
| 141 | + * @throws DSOutOfServiceException If the connection is broken, or not logged in |
| 142 | + * @throws DSAccessException If an error occurred while trying to retrieve data from OMERO |
| 143 | + * service. |
| 144 | + */ |
| 145 | + public BufferedImage renderPlaneAsBufferedImage(SecurityContext ctx, long imageId, int z, int t) |
| 146 | + throws DSOutOfServiceException, DSAccessException { |
| 147 | + try { |
| 148 | + ImageData img = gateway.getFacility(LoadFacility.class).getImage(ctx, imageId); |
| 149 | + int[] pixels = renderPlane(ctx, imageId, 0, 0); |
| 150 | + int w = img.getDefaultPixels().getSizeX(); |
| 151 | + int h = img.getDefaultPixels().getSizeY(); |
| 152 | + BufferedImage image = new BufferedImage(w, h, |
| 153 | + BufferedImage.TYPE_INT_ARGB); |
| 154 | + image.setRGB(0, 0, w, h, pixels, 0, w); |
| 155 | + return image; |
| 156 | + } catch (Throwable e) { |
| 157 | + handleException(this, e, "Could not render plane."); |
| 158 | + } |
| 159 | + return null; |
| 160 | + } |
| 161 | +} |
0 commit comments