Skip to content

Commit

Permalink
Merge branch '#262_CreateImageView'
Browse files Browse the repository at this point in the history
  • Loading branch information
lfcnassif committed Feb 14, 2025
2 parents f3e833c + 29755b2 commit 8adae91
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
9 changes: 8 additions & 1 deletion iped-app/resources/config/conf/ImageThumbsConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ galleryThreads = default

# Logs rendering of each image in gallery. Could generate huge logs or
# slow down gallery rendering depending on log location.
logGalleryRendering = false
logGalleryRendering = false

# Create view images (to speed up rendering) for the following mime types.
# Used only for images that require external conversion. Use ";" as separator.
mimesToCreateView = image/heic; image/heif

# Maximum size (in pixels) of generated view image
maxViewImageSize = 2400
5 changes: 4 additions & 1 deletion iped-app/resources/scripts/tasks/FaceRecognitionTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ def process(self, item):
isVideo = False
mediaType = item.getMediaType().toString()
if mediaType.startswith('image'):
img_path = item.getTempFile().getAbsolutePath()
if item.getViewFile() is not None and os.path.exists(item.getViewFile().getAbsolutePath()):
img_path = item.getViewFile().getAbsolutePath()
else:
img_path = item.getTempFile().getAbsolutePath()
elif mediaType.startswith('video') and not FaceRecognitionTask.videoSubitems:
img_path = item.getViewFile().getAbsolutePath()
isVideo = True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package iped.engine.config;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import iped.utils.UTF8Properties;

public class ImageThumbTaskConfig extends AbstractTaskPropertiesConfig {

/**
*
*/
private static final long serialVersionUID = 1L;

private static final String ENABLE_PROP = "enableImageThumbs"; //$NON-NLS-1$
Expand All @@ -24,6 +25,8 @@ public class ImageThumbTaskConfig extends AbstractTaskPropertiesConfig {
private int lowResDensity = 96;
private int highResDensity = 250;
private int maxMPixelsInMemory = 32;
private int maxViewImageSize = 2400;
private final Set<String> mimesToCreateView = new HashSet<String>();

public boolean isEnableExternalConv() {
return enableExternalConv;
Expand Down Expand Up @@ -69,6 +72,14 @@ public int getMaxMPixelsInMemory() {
return maxMPixelsInMemory;
}

public int getMaxViewImageSize() {
return maxViewImageSize;
}

public Set<String> getMimesToCreateView() {
return Collections.unmodifiableSet(mimesToCreateView);
}

@Override
public String getTaskEnableProperty() {
return ENABLE_PROP;
Expand Down Expand Up @@ -141,6 +152,17 @@ public void processProperties(UTF8Properties properties) {
maxMPixelsInMemory = Integer.valueOf(value.trim());
}

}
value = properties.getProperty("mimesToCreateView");
if (value != null) {
String[] mimes = value.split(";");
for (String mime : mimes) {
mimesToCreateView.add(mime.trim());
}
}

value = properties.getProperty("maxViewImageSize");
if (value != null && !value.trim().isEmpty()) {
maxViewImageSize = Integer.valueOf(value.trim());
}
}
}
72 changes: 60 additions & 12 deletions iped-engine/src/main/java/iped/engine/task/ImageThumbTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand All @@ -28,6 +29,7 @@
import iped.engine.config.Configuration;
import iped.engine.config.ConfigurationManager;
import iped.engine.config.ImageThumbTaskConfig;
import iped.engine.util.Util;
import iped.properties.MediaTypes;
import iped.utils.ExternalImageConverter;
import iped.utils.ImageUtil;
Expand All @@ -37,7 +39,7 @@
public class ImageThumbTask extends ThumbTask {

public static final String THUMB_TIMEOUT = "thumbTimeout"; //$NON-NLS-1$

private static final int TIMEOUT_DELTA = 5;

private static final int samplingRatio = 3;
Expand All @@ -58,6 +60,8 @@ public class ImageThumbTask extends ThumbTask {
private static final AtomicBoolean extConvPropInit = new AtomicBoolean(false);

private int thumbSize;
private int maxViewImageSize;
private Set<String> mimesToCreateView;

public ImageThumbTaskConfig getImageThumbConfig() {
return imgThumbConfig;
Expand Down Expand Up @@ -86,7 +90,7 @@ public void init(ConfigurationManager configurationManager) throws Exception {
System.setProperty(ExternalImageConverter.winToolPathPrefixProp,
Configuration.getInstance().appRoot);
}

File tmpDir = new File(System.getProperty("java.io.tmpdir"), "ext-conv"); //$NON-NLS-1$ //$NON-NLS-2$
tmpDir.mkdirs();
System.setProperty(ExternalImageConverter.tmpDirProp, tmpDir.getAbsolutePath());
Expand All @@ -98,6 +102,8 @@ public void init(ConfigurationManager configurationManager) throws Exception {

externalImageConverter = new ExternalImageConverter(executor);
thumbSize = imgThumbConfig.getThumbSize();
maxViewImageSize = imgThumbConfig.getMaxViewImageSize();
mimesToCreateView = imgThumbConfig.getMimesToCreateView();

synchronized (logInit) {
if (isEnabled() && !logInit.get()) {
Expand Down Expand Up @@ -229,6 +235,10 @@ protected void process(IItem evidence) throws Exception {

File thumbFile = getThumbFile(evidence);
if (hasThumb(evidence, thumbFile)) {
File viewFile = getViewFile(evidence, "jpg");
if (viewFile.exists()) {
evidence.setViewFile(viewFile);
}
return;
}

Expand Down Expand Up @@ -277,8 +287,13 @@ public static boolean isJpeg(IItem item) {
}

private void createImageThumb(IItem evidence, File thumbFile) {
long[] performanceStats = new long[numStats];
try {
if (evidence.getLength() != null && evidence.getLength().longValue() == 0) {
// If evidence length is zero, don't even try to create a thumb
saveThumb(evidence, thumbFile);
return;
}
long[] performanceStats = new long[numStats];
BufferedImage img = null;
if (imgThumbConfig.isExtractThumb() && isJpeg(evidence)) { // $NON-NLS-1$
long t = System.currentTimeMillis();
Expand All @@ -300,15 +315,44 @@ private void createImageThumb(IItem evidence, File thumbFile) {
performanceStats[img == null ? 7 : 5] += System.currentTimeMillis() - t;
}
if (img == null) {
// External Conversion
long t = System.currentTimeMillis();
try (BufferedInputStream stream = evidence.getBufferedInputStream()) {
img = externalImageConverter.getImage(stream, thumbSize, false, evidence.getLength(), true);
if (img != null)
evidence.setExtraAttribute("externalThumb", "true"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (TimeoutException e) {
stats.incTimeouts();
evidence.setExtraAttribute(THUMB_TIMEOUT, "true"); //$NON-NLS-1$
logger.warn("Timeout creating thumb: " + evidence); //$NON-NLS-1$

// Create a high resolution view
String mime = MediaTypes.getMimeTypeString(evidence);
if (mime != null && mimesToCreateView.contains(mime)) {
try (BufferedInputStream stream = evidence.getBufferedInputStream()) {
img = externalImageConverter.getImage(stream, maxViewImageSize, true, evidence.getLength(),
true);
} catch (TimeoutException e) {
stats.incTimeouts();
evidence.setExtraAttribute(THUMB_TIMEOUT, "true");
logger.warn("Timeout creating view: " + evidence);
}
if (img != null) {
// Store view
File viewTmpFile = getViewFile(evidence, "tmp");
viewTmpFile.getParentFile().mkdirs();
ImageIO.write(img, "jpg", viewTmpFile);
File viewFile = getViewFile(evidence, "jpg");
viewTmpFile.renameTo(viewFile);
evidence.setViewFile(viewFile);
}
}

if (img == null) {
// No view was created through external conversion
try (BufferedInputStream stream = evidence.getBufferedInputStream()) {
img = externalImageConverter.getImage(stream, thumbSize, false, evidence.getLength(), true);
} catch (TimeoutException e) {
stats.incTimeouts();
evidence.setExtraAttribute(THUMB_TIMEOUT, "true");
logger.warn("Timeout creating thumb: " + evidence);
}
}

if (img != null) {
evidence.setExtraAttribute("externalThumb", "true");
}
performanceStats[img == null ? 10 : 8]++;
performanceStats[img == null ? 11 : 9] += System.currentTimeMillis() - t;
Expand Down Expand Up @@ -371,4 +415,8 @@ private void createImageThumb(IItem evidence, File thumbFile) {
updateHasThumb(evidence);
}
}
}

private File getViewFile(IItem evidence, String ext) {
return Util.getFileFromHash(new File(output, MakePreviewTask.viewFolder), evidence.getHash(), ext);
}
}

0 comments on commit 8adae91

Please sign in to comment.