algo changeLuminosity marche! Il faut faire les exceptions

This commit is contained in:
keahorvath committed 2023-03-14 21:16:30 +01:00
1 parent dbaf3401b8
commit c058036459
1 file changed
+33 -38
@@ -1,13 +1,14 @@
package pdl.backend; package pdl.backend;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Map;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.print.attribute.standard.Media;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -33,7 +34,6 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
@RestController @RestController
public class ImageController { public class ImageController {
@@ -93,52 +93,47 @@ public class ImageController {
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, @GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE,
MediaType.IMAGE_PNG_VALUE }) MediaType.IMAGE_PNG_VALUE })
public ResponseEntity<?> applyAlgorithm(@PathVariable("id") long id, public ResponseEntity<?> getImage(@PathVariable("id") long id,
@RequestParam(required = false) String algorithm, @RequestParam(required = false) Map<String, String> parameters) {
@RequestParam(required = false) Long p1, @RequestParam(required = false) Long p2) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
if (algorithm == null) { if (parameters.isEmpty()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData()); InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(image.get().getMediaType()) return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream)); .body(new InputStreamResource(inputStream));
} }
if (algorithm.equals("changeLuminosity")) { if (parameters.containsKey("algorithm")) {
BufferedImage bufImg; if (parameters.get("algorithm").equals("changeLuminosity") && parameters.containsKey("delta")) {
try { BufferedImage bufImg;
bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData())); try {
} catch (IOException e) { bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData()));
return new ResponseEntity<>("Image not readable", } catch (IOException e) {
HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>("Image not readable",
HttpStatus.INTERNAL_SERVER_ERROR);
}
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
true, GrayU8.class);
ImageProcessing.changeLuminosity(input, Integer.parseInt(parameters.get("delta")));
bufImg = ConvertBufferedImage.convertTo_U8(input, null, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bufImg, image.get().getMediaType().getSubtype(), baos);
} catch (IOException e) {
return new ResponseEntity<>("Image couldn't be converted",
HttpStatus.INTERNAL_SERVER_ERROR);
}
byte[] imageData = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(imageData);
return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream));
} }
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
true, GrayU8.class);
ImageProcessing.changeLuminosity(input, (int) p1.longValue());
int width = input.getWidth();
int height = input.getHeight();
int numChannels = input.getNumBands();
// Allocate a byte array to hold the pixel data
byte[] imageData = new byte[width * height * numChannels];
// Iterate over the planes and copy the pixel data into the byte array
for (int i = 0; i < numChannels; i++) {
byte[] planeData = input.getBand(i).getData();
System.arraycopy(planeData, 0, imageData, i * width * height, width *
height);
}
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
// InputStream inputStream = new ByteArrayInputStream(imageData);
System.out.println("hi");
return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream));
} }
} }
return new ResponseEntity<>("Image id=" + id + " not found.", return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
HttpStatus.NOT_FOUND);
} }
} }