Feature/convert to grayscale (#3003)
# Description of Changes Please provide a summary of the changes, including: - What was changed -Grayscale Image Compression: Modified the compressImagesInPDF method to optionally convert images to grayscale during compression. This is achieved by checking if the grayScale flag is true (or if the image is already in grayscale) and then processing the image accordingly. UI Update: Updated the compress-pdf.html file by adding a checkbox for grayscale compression. Additionally, a new translation key compress.grayscale.label with the text "Apply Grayscale for Compression" has been added across all supported languages. - Why the change was made -Enhanced Compression Options: This feature provides users with an option to compress images in PDFs more effectively by reducing the color complexity, which can lead to smaller file sizes. Improved Flexibility: It allows users to decide whether they want to maintain the original color images or opt for a grayscale version. - Any challenges encountered - The translation for compress.grayscale.label was generated using an automated translator, so it might not be completely accurate and could require further review. Closes #2603 --- ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [x] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
This commit is contained in:
@@ -31,6 +31,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.SPDF.model.api.misc.OptimizePdfRequest;
|
||||
import stirling.software.SPDF.service.CustomPDDocumentFactory;
|
||||
import stirling.software.SPDF.utils.GeneralUtils;
|
||||
@@ -51,7 +52,7 @@ public class CompressController {
|
||||
this.pdfDocumentFactory = pdfDocumentFactory;
|
||||
}
|
||||
|
||||
private void compressImagesInPDF(Path pdfFile, double initialScaleFactor) throws Exception {
|
||||
private void compressImagesInPDF(Path pdfFile, double initialScaleFactor, boolean grayScale) throws Exception {
|
||||
byte[] fileBytes = Files.readAllBytes(pdfFile);
|
||||
try (PDDocument doc = Loader.loadPDF(fileBytes)) {
|
||||
double scaleFactor = initialScaleFactor;
|
||||
@@ -76,11 +77,23 @@ public class CompressController {
|
||||
bufferedImage.getScaledInstance(
|
||||
newWidth, newHeight, Image.SCALE_SMOOTH);
|
||||
|
||||
BufferedImage scaledBufferedImage =
|
||||
new BufferedImage(
|
||||
newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||
scaledBufferedImage.getGraphics().drawImage(scaledImage, 0, 0, null);
|
||||
|
||||
BufferedImage scaledBufferedImage;
|
||||
if (grayScale
|
||||
|| bufferedImage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
|
||||
scaledBufferedImage =
|
||||
new BufferedImage(
|
||||
newWidth, newHeight, BufferedImage.TYPE_BYTE_GRAY);
|
||||
scaledBufferedImage
|
||||
.getGraphics()
|
||||
.drawImage(scaledImage, 0, 0, null);
|
||||
} else {
|
||||
scaledBufferedImage =
|
||||
new BufferedImage(
|
||||
newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||
scaledBufferedImage
|
||||
.getGraphics()
|
||||
.drawImage(scaledImage, 0, 0, null);
|
||||
}
|
||||
ByteArrayOutputStream compressedImageStream =
|
||||
new ByteArrayOutputStream();
|
||||
ImageIO.write(scaledBufferedImage, "jpeg", compressedImageStream);
|
||||
@@ -139,6 +152,7 @@ public class CompressController {
|
||||
}
|
||||
|
||||
boolean sizeMet = false;
|
||||
boolean grayscaleEnabled = Boolean.TRUE.equals(request.getGrayscale());
|
||||
while (!sizeMet && optimizeLevel <= 9) {
|
||||
|
||||
// Apply additional image compression for levels 6-9
|
||||
@@ -152,7 +166,7 @@ public class CompressController {
|
||||
case 9 -> 0.5; // 60% of original size
|
||||
default -> 1.0;
|
||||
};
|
||||
compressImagesInPDF(tempInputFile, scaleFactor);
|
||||
compressImagesInPDF(tempInputFile, scaleFactor, grayscaleEnabled);
|
||||
}
|
||||
|
||||
// Run QPDF optimization
|
||||
@@ -169,6 +183,7 @@ public class CompressController {
|
||||
command.add("--compression-level=" + optimizeLevel);
|
||||
command.add("--compress-streams=y");
|
||||
command.add("--object-streams=generate");
|
||||
command.add("--no-warn");
|
||||
command.add(tempInputFile.toString());
|
||||
command.add(tempOutputFile.toString());
|
||||
|
||||
|
||||
@@ -29,4 +29,13 @@ public class OptimizePdfRequest extends PDFFile {
|
||||
"Whether to normalize the PDF content for better compatibility. Default is false.",
|
||||
defaultValue = "false")
|
||||
private Boolean normalize = false;
|
||||
|
||||
@Schema(
|
||||
description = "Whether to convert the PDF to grayscale. Default is false.",
|
||||
defaultValue = "false")
|
||||
private Boolean grayscale = false;
|
||||
|
||||
public Boolean getGrayscale() {
|
||||
return grayscale;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user