Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ImageTagsUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.contentviewers.imagetagging;
20 
21 import java.awt.image.BufferedImage;
22 import java.io.BufferedInputStream;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.util.Collection;
27 import java.util.concurrent.ExecutionException;
28 import javafx.concurrent.Task;
29 import javafx.embed.swing.SwingFXUtils;
30 import javafx.scene.image.Image;
31 import javax.imageio.ImageIO;
32 import org.opencv.core.Core;
33 import org.opencv.core.Mat;
34 import org.opencv.core.MatOfByte;
35 import org.opencv.core.Point;
36 import org.opencv.core.Scalar;
37 import org.opencv.core.Size;
38 import org.opencv.highgui.Highgui;
39 import org.opencv.imgproc.Imgproc;
41 import org.sleuthkit.datamodel.AbstractFile;
42 import org.sleuthkit.datamodel.ReadContentInputStream;
43 
47 public final class ImageTagsUtil {
48 
49  //String constant for writing PNG in ImageIO
50  private final static String AWT_PNG = "png";
51 
52  //String constant for encoding PNG in OpenCV
53  private final static String OPENCV_PNG = ".png";
54 
66  public static BufferedImage getImageWithTags(AbstractFile file,
67  Collection<ImageTagRegion> tagRegions) throws IOException, InterruptedException, ExecutionException {
68 
69  //The raw image in OpenCV terms
70  Mat sourceImage = getImageMatFromFile(file);
71  //Image with tags in OpenCV terms
72  MatOfByte taggedMatrix = getTaggedImageMatrix(sourceImage, tagRegions);
73 
74  try (ByteArrayInputStream taggedStream = new ByteArrayInputStream(taggedMatrix.toArray())) {
75  return ImageIO.read(taggedStream);
76  } finally {
77  sourceImage.release();
78  taggedMatrix.release();
79  }
80  }
81 
91  private static BufferedImage getImageFromFile(AbstractFile file) throws IOException, InterruptedException, ExecutionException {
92  if (ImageUtils.isGIF(file)) {
93  //Grab the first frame.
94  try (BufferedInputStream bufferedReadContentStream =
95  new BufferedInputStream(new ReadContentInputStream(file))) {
96  return ImageIO.read(bufferedReadContentStream);
97  }
98  } else {
99  //Otherwise, read the full image.
100  Task<Image> readImageTask = ImageUtils.newReadImageTask(file);
101  readImageTask.run();
102  Image fxResult = readImageTask.get();
103  return SwingFXUtils.fromFXImage(fxResult, null);
104  }
105  }
106 
117  private static Mat getImageMatFromFile(AbstractFile file) throws InterruptedException, ExecutionException, IOException {
118  //Get image from file
119  BufferedImage buffImage = getImageFromFile(file);
120 
121  //Convert it to OpenCV Mat.
122  try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
123  ImageIO.write(buffImage, AWT_PNG, outStream);
124 
125  byte[] imageBytes = outStream.toByteArray();
126  MatOfByte rawSourceBytes = new MatOfByte(imageBytes);
127  Mat sourceImage = Highgui.imdecode(rawSourceBytes, Highgui.IMREAD_COLOR);
128  rawSourceBytes.release();
129 
130  return sourceImage;
131  }
132  }
133 
141  private static MatOfByte getTaggedImageMatrix(Mat sourceImage, Collection<ImageTagRegion> tagRegions) {
142 
143  //Apply all tags to source image
144  for (ImageTagRegion region : tagRegions) {
145  Point topLeft = new Point(region.getX(), region.getY());
146  Point bottomRight = new Point(topLeft.x + region.getWidth(),
147  topLeft.y + region.getHeight());
148  //Red
149  Scalar rectangleBorderColor = new Scalar(0, 0, 255);
150 
151  int rectangleBorderWidth = (int) Math.rint(region.getStrokeThickness());
152 
153  Core.rectangle(sourceImage, topLeft, bottomRight,
154  rectangleBorderColor, rectangleBorderWidth);
155  }
156 
157  MatOfByte taggedMatrix = new MatOfByte();
158  Highgui.imencode(OPENCV_PNG, sourceImage, taggedMatrix);
159 
160  return taggedMatrix;
161  }
162 
174  public static BufferedImage getThumbnailWithTags(AbstractFile file, Collection<ImageTagRegion> tagRegions,
175  IconSize iconSize) throws IOException, InterruptedException, ExecutionException {
176 
177  //Raw image
178  Mat sourceImage = getImageMatFromFile(file);
179  //Full size image with tags
180  MatOfByte taggedMatrix = getTaggedImageMatrix(sourceImage, tagRegions);
181  //Resized to produce thumbnail
182  MatOfByte thumbnailMatrix = getResizedMatrix(taggedMatrix, iconSize);
183 
184  try (ByteArrayInputStream thumbnailStream = new ByteArrayInputStream(thumbnailMatrix.toArray())) {
185  return ImageIO.read(thumbnailStream);
186  } finally {
187  sourceImage.release();
188  taggedMatrix.release();
189  thumbnailMatrix.release();
190  }
191  }
192 
201  private static MatOfByte getResizedMatrix(MatOfByte taggedMatrix, IconSize size) {
202  Size resizeDimensions = new Size(size.getSize(), size.getSize());
203  Mat taggedImage = Highgui.imdecode(taggedMatrix, Highgui.IMREAD_COLOR);
204 
205  Mat thumbnailImage = new Mat();
206  Imgproc.resize(taggedImage, thumbnailImage, resizeDimensions);
207 
208  MatOfByte thumbnailMatrix = new MatOfByte();
209  Highgui.imencode(OPENCV_PNG, thumbnailImage, thumbnailMatrix);
210 
211  thumbnailImage.release();
212  taggedImage.release();
213 
214  return thumbnailMatrix;
215  }
216 
217  private ImageTagsUtil() {
218  }
219 
223  public enum IconSize {
224  SMALL(50),
225  MEDIUM(100),
226  LARGE(200);
227 
228  private final int SIZE;
229 
230  IconSize(int size) {
231  this.SIZE = size;
232  }
233 
234  public int getSize() {
235  return SIZE;
236  }
237  }
238 }
static boolean isGIF(AbstractFile file)
static MatOfByte getResizedMatrix(MatOfByte taggedMatrix, IconSize size)
static BufferedImage getImageWithTags(AbstractFile file, Collection< ImageTagRegion > tagRegions)
static Task< javafx.scene.image.Image > newReadImageTask(AbstractFile file)
static BufferedImage getImageFromFile(AbstractFile file)
static BufferedImage getThumbnailWithTags(AbstractFile file, Collection< ImageTagRegion > tagRegions, IconSize iconSize)
static MatOfByte getTaggedImageMatrix(Mat sourceImage, Collection< ImageTagRegion > tagRegions)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Oct 4 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.