| 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 | import static java.util.Optional.ofNullable; | |
| 16 | ||
| 17 | /// Utility class for image-related operations such as format detection and | |
| 18 | /// content type mapping. | |
| 19 | public class ImgUtils { | |
| 20 | ||
| 21 | private static final Logger log = LoggerFactory.getLogger(ImgUtils.class); | |
| 22 | ||
| 23 | private ImgUtils() {throw new IllegalStateException("Utility class");} | |
| 24 | ||
| 25 | /// Detects the format and dimensions of an image from its byte content. | |
| 26 | /// | |
| 27 | /// @param bytes the byte content of the image | |
| 28 | /// | |
| 29 | /// @return an [Optional] containing the detected [ImgFormat], or empty if | |
| 30 | /// no reader is found | |
| 31 | /// | |
| 32 | /// @throws UtilsException if an I/O error occurs during format detection | |
| 33 | public static Optional<ImgFormat> detectFormat(byte[] bytes) { | |
| 34 | var inputStream = new ByteArrayInputStream(bytes); | |
| 35 | try ( | |
| 36 | var imageInputStream = ImageIO.createImageInputStream( | |
| 37 | inputStream) | |
| 38 | ) { | |
| 39 | var readers = ImageIO.getImageReaders(imageInputStream); | |
| 40 |
1
1. detectFormat : negated conditional → NO_COVERAGE |
if (!readers.hasNext()) { |
| 41 | log.debug("Could not find an image reader for this file"); | |
| 42 | return Optional.empty(); | |
| 43 | } | |
| 44 | var reader = readers.next(); | |
| 45 |
1
1. detectFormat : removed call to javax/imageio/ImageReader::setInput → NO_COVERAGE |
reader.setInput(imageInputStream, false, false); |
| 46 | var formatName = reader.getFormatName(); | |
| 47 | var width = reader.getWidth(0); | |
| 48 | var height = reader.getHeight(0); | |
| 49 | var imgFormat = new ImgFormat(formatName, | |
| 50 | new Dimension(width, height)); | |
| 51 |
1
1. detectFormat : removed call to javax/imageio/ImageReader::dispose → NO_COVERAGE |
reader.dispose(); |
| 52 |
1
1. detectFormat : replaced return value with Optional.empty for pro/verron/officestamper/utils/image/ImgUtils::detectFormat → NO_COVERAGE |
return Optional.of(imgFormat); |
| 53 | } catch (IOException e) { | |
| 54 | throw new UtilsException(e); | |
| 55 | } | |
| 56 | ||
| 57 | } | |
| 58 | ||
| 59 | /// Returns the MIME content type for a given image type string. | |
| 60 | /// | |
| 61 | /// Supported types include: emf, svg, wmf, tif, png, jpeg, gif, bmp. | |
| 62 | /// | |
| 63 | /// @param imageType the image type string (case-insensitive) | |
| 64 | /// | |
| 65 | /// @return an [Optional] containing the MIME content type, or empty if the | |
| 66 | /// type is not supported | |
| 67 | public static Optional<String> supportedType(String imageType) { | |
| 68 | var supportedImageTypes = new HashMap<String, String>(); | |
| 69 | supportedImageTypes.put("emf", ContentTypes.IMAGE_EMF); | |
| 70 | supportedImageTypes.put("svg", ContentTypes.IMAGE_SVG); | |
| 71 | supportedImageTypes.put("wmf", ContentTypes.IMAGE_WMF); | |
| 72 | supportedImageTypes.put("tif", ContentTypes.IMAGE_TIFF); | |
| 73 | supportedImageTypes.put("png", ContentTypes.IMAGE_PNG); | |
| 74 | supportedImageTypes.put("jpeg", ContentTypes.IMAGE_JPEG); | |
| 75 | supportedImageTypes.put("gif", ContentTypes.IMAGE_GIF); | |
| 76 | supportedImageTypes.put("bmp", ContentTypes.IMAGE_BMP); | |
| 77 | var normalizedType = imageType.toLowerCase(); | |
| 78 | var mimeType = supportedImageTypes.get(normalizedType); | |
| 79 |
1
1. supportedType : replaced return value with Optional.empty for pro/verron/officestamper/utils/image/ImgUtils::supportedType → NO_COVERAGE |
return ofNullable(mimeType); |
| 80 | } | |
| 81 | } | |
Mutations | ||
| 40 |
1.1 |
|
| 45 |
1.1 |
|
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 79 |
1.1 |