| 1 | package pro.verron.officestamper.utils.image; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.contenttype.ContentTypes; | |
| 4 | import org.slf4j.Logger; | |
| 5 | import org.slf4j.LoggerFactory; | |
| 6 | import pro.verron.officestamper.utils.UtilsException; | |
| 7 | ||
| 8 | import javax.imageio.ImageIO; | |
| 9 | import java.awt.*; | |
| 10 | import java.io.ByteArrayInputStream; | |
| 11 | import java.io.IOException; | |
| 12 | import java.util.HashMap; | |
| 13 | import java.util.Optional; | |
| 14 | ||
| 15 | public class ImgUtils { | |
| 16 | private static final Logger log = LoggerFactory.getLogger(ImgUtils.class); | |
| 17 | ||
| 18 | public static Optional<ImgFormat> detectFormat(byte[] bytes) { | |
| 19 | var inputStream = new ByteArrayInputStream(bytes); | |
| 20 | try (var imageInputStream = ImageIO.createImageInputStream(inputStream)) { | |
| 21 | var readers = ImageIO.getImageReaders(imageInputStream); | |
| 22 |
1
1. detectFormat : negated conditional → NO_COVERAGE |
if (!readers.hasNext()) { |
| 23 | log.debug("Could not find an image reader for this file"); | |
| 24 | return Optional.empty(); | |
| 25 | } | |
| 26 | var reader = readers.next(); | |
| 27 |
1
1. detectFormat : removed call to javax/imageio/ImageReader::setInput → NO_COVERAGE |
reader.setInput(imageInputStream, false, false); |
| 28 | var formatName = reader.getFormatName(); | |
| 29 | var width = reader.getWidth(0); | |
| 30 | var height = reader.getHeight(0); | |
| 31 | var imgFormat = new ImgFormat(formatName, new Dimension(width, height)); | |
| 32 |
1
1. detectFormat : removed call to javax/imageio/ImageReader::dispose → NO_COVERAGE |
reader.dispose(); |
| 33 |
1
1. detectFormat : replaced return value with Optional.empty for pro/verron/officestamper/utils/image/ImgUtils::detectFormat → NO_COVERAGE |
return Optional.of(imgFormat); |
| 34 | } catch (IOException e) { | |
| 35 | throw new UtilsException(e); | |
| 36 | } | |
| 37 | ||
| 38 | } | |
| 39 | ||
| 40 | public static Optional<String> supportedContentType(String imageType) { | |
| 41 | var supportedImageTypes = new HashMap<String, String>(); | |
| 42 | supportedImageTypes.put("emf", ContentTypes.IMAGE_EMF); | |
| 43 | supportedImageTypes.put("svg", ContentTypes.IMAGE_SVG); | |
| 44 | supportedImageTypes.put("wmf", ContentTypes.IMAGE_WMF); | |
| 45 | supportedImageTypes.put("tif", ContentTypes.IMAGE_TIFF); | |
| 46 | supportedImageTypes.put("png", ContentTypes.IMAGE_PNG); | |
| 47 | supportedImageTypes.put("jpeg", ContentTypes.IMAGE_JPEG); | |
| 48 | supportedImageTypes.put("gif", ContentTypes.IMAGE_GIF); | |
| 49 | supportedImageTypes.put("bmp", ContentTypes.IMAGE_BMP); | |
| 50 |
1
1. supportedContentType : replaced return value with Optional.empty for pro/verron/officestamper/utils/image/ImgUtils::supportedContentType → NO_COVERAGE |
return Optional.ofNullable(supportedImageTypes.get(imageType.toLowerCase())); |
| 51 | } | |
| 52 | } | |
Mutations | ||
| 22 |
1.1 |
|
| 27 |
1.1 |
|
| 32 |
1.1 |
|
| 33 |
1.1 |
|
| 50 |
1.1 |