changes + add old code

This commit is contained in:
Theophyl Hoguet committed 2023-03-15 19:43:34 +01:00
1 parent aa9229f432
commit 11b64ebd9b
1 file changed
+123 -149
@@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.Map;
@@ -32,6 +33,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.awt.image.BufferedImage;
@@ -49,30 +51,32 @@ public class ImageController {
@DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) {
imageDao.delete(image.get());
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
var img = imageDao.retrieve(id);
if (img.isEmpty())
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
imageDao.delete(img.get());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@PostMapping(value = "/images")
@PostMapping(value = "/images", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
String contentType = file.getContentType();
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())
&& !contentType.equals(MediaType.IMAGE_PNG.toString())) {
return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
if (contentType == null
|| (!contentType.equals(MediaType.IMAGE_JPEG_VALUE) && !contentType.equals(MediaType.IMAGE_PNG_VALUE)))
return new ResponseEntity<>(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
Image newImg = new Image(file.getOriginalFilename(), file.getBytes());
imageDao.create(newImg);
URI newURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/" + newImg.getId()).build().toUri();
return ResponseEntity.created(newURI).build();
} catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
return new ResponseEntity<>(HttpStatus.NOT_MODIFIED);
}
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
}
@DeleteMapping(value = "/images")
public ResponseEntity<?> deleteImagesList() {
return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
}
@GetMapping(value = "/images", produces = "application/json")
@@ -91,140 +95,110 @@ public class ImageController {
return nodes;
}
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE,
MediaType.IMAGE_PNG_VALUE })
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE })
public ResponseEntity<?> getImage(@PathVariable("id") long id,
@RequestParam(required = false) Map<String, String> parameters) {
Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) {
if (parameters.isEmpty()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(image.get().getMediaType())
.body(new InputStreamResource(inputStream));
}
if (parameters.containsKey("algorithm")) {
String algo = parameters.get("algorithm");
BufferedImage bufImg;
try {
bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData()));
} catch (IOException e) {
return new ResponseEntity<>("Image not readable",
HttpStatus.INTERNAL_SERVER_ERROR);
}
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
true, GrayU8.class);
if (algo.equals("changeLuminosity")) {
if (parameters.containsKey("delta")
&& parameters.size() == 2) {
try {
int i = Integer.parseInt(parameters.get("delta"));
ImageProcessing.changeLuminosity(input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'delta' should be an integer",
HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(
"Algorithm 'changeLuminosity' requires one parameter 'delta'(integer)",
HttpStatus.BAD_REQUEST);
}
} else if (algo.equals("histogram")) {
if (parameters.size() == 1) {
ImageProcessing.histogram(input);
} else {
return new ResponseEntity<>(
"Algorithm 'histogram' requires no parameters",
HttpStatus.BAD_REQUEST);
}
} else if (algo.equals("colorFilter")) {
if (parameters.containsKey("hue") && parameters.size() == 2) {
try {
int i = Integer.parseInt(parameters.get("hue"));
ImageProcessing.colorFilter(i, input);
// hue entre 0 et 360?
} catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'hue' should be an integer",
HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(
"Algorithm 'colorFilter' requires one parameter 'hue'(integer)",
HttpStatus.BAD_REQUEST);
}
} else if (algo.equals("meanFilter")) {
if (parameters.containsKey("size") && parameters.size() == 2) {
try {
int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST);
}
Planar<GrayU8> clone = input.clone();
ImageProcessing.meanFilter(clone, input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(
"Algorithm 'meanFilter' requires one parameter 'size'(odd positive integer)",
HttpStatus.BAD_REQUEST);
}
} else if (algo.equals("gaussienFilter")) {
if (parameters.containsKey("size") && parameters.size() == 2) {
try {
int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST);
}
Planar<GrayU8> clone = input.clone();
ImageProcessing.gaussienFilter(clone, input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>("Parameter 'size' should be an odd positive integer",
HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(
"Algorithm 'gaussienFilter' requires one parameter 'size'(odd positive integer)",
HttpStatus.BAD_REQUEST);
}
} else if (algo.equals("contours")) {
if (parameters.size() == 1) {
Planar<GrayU8> clone = input.clone();
ImageProcessing.gradientImageSobel(clone, input);
} else {
return new ResponseEntity<>(
"Algorithm 'contours' requires no parameters",
HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(
"The requested algorithm '" + parameters.get("algorithm") + "' doesn't exist",
HttpStatus.BAD_REQUEST);
}
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));
} else {
return new ResponseEntity<>("First parameter should be 'algorithm",
HttpStatus.BAD_REQUEST);
}
// Récupère l'image correspondante à l'ID fourni
Optional<Image> optImage = imageDao.retrieve(id);
if (!optImage.isPresent())
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Image image = optImage.get();
// Si aucun paramètre n'est fourni, renvoie l'image brute
if (parameters.isEmpty()) {
InputStream inputStream = new ByteArrayInputStream(image.getData());
return ResponseEntity.ok().contentType(image.getMediaType())
.body(new InputStreamResource(inputStream));
}
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
// Vérifie la présence du paramètre "algorithm"
if (!parameters.containsKey("algorithm"))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
String algo = parameters.get("algorithm");
BufferedImage bufImg;
try {
bufImg = ImageIO.read(new ByteArrayInputStream(image.getData()));
} catch (IOException e) {
System.out.println("Error while reading image");
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null, true, GrayU8.class);
// applique l'algo a l'image en fonction des parametres et verifie la validite
// des parametres
switch (algo) {
case "changeLuminosity":
if (!parameters.containsKey("delta") || parameters.size() != 2)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
try {
int i = Integer.parseInt(parameters.get("delta"));
ImageProcessing.changeLuminosity(input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
break;
case "histogram":
if (parameters.size() != 1)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
ImageProcessing.histogram(input);
break;
case "colorFilter":
if (!parameters.containsKey("hue") || parameters.size() != 2)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
try {
int i = Integer.parseInt(parameters.get("hue"));
ImageProcessing.colorFilter(i, input);
// hue entre 0 et 360?
} catch (NumberFormatException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
break;
case "meanFilter":
if (!parameters.containsKey("size") || parameters.size() != 2)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
try {
int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Planar<GrayU8> clone = input.clone();
ImageProcessing.meanFilter(clone, input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
break;
case "gaussienFilter":
if (!parameters.containsKey("size") || parameters.size() != 2)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
try {
int i = Integer.parseInt(parameters.get("size"));
if (i % 2 == 0)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Planar<GrayU8> clone = input.clone();
ImageProcessing.gaussienFilter(clone, input, i);
} catch (NumberFormatException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
break;
case "contours":
if (parameters.size() != 1)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Planar<GrayU8> clone = input.clone();
ImageProcessing.gradientImageSobel(clone, input);
break;
default:
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
// transforme l'image en quelque chose que le navigateur puisse lire
bufImg = ConvertBufferedImage.convertTo_U8(input, null, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bufImg, image.getMediaType().getSubtype(), baos);
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
byte[] imageData = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(imageData);
return ResponseEntity.ok().contentType(image.getMediaType()).body(new InputStreamResource(inputStream));
}
}
}