removed unnessary imports and main in ImageProcessing and started besoin 8

This commit is contained in:
keahorvath authored and Theophyl Hoguet committed 2023-03-15 19:53:01 +01:00
1 parent 7819411212
commit 24a3e9d6e7
2 files changed
+108 -102

No files matched your search

@@ -6,12 +6,17 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.imageio.ImageIO;
import javax.print.attribute.standard.Media; 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;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.struct.image.GrayU8;
import boofcv.struct.image.Planar;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@@ -27,85 +32,113 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; 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.DataBufferInt;
@RestController @RestController
public class ImageController { public class ImageController {
@Autowired @Autowired
private ObjectMapper mapper; private ObjectMapper mapper;
private final ImageDao imageDao; private final ImageDao imageDao;
public ImageController(ImageDao imageDao) { public ImageController(ImageDao imageDao) {
this.imageDao = imageDao; this.imageDao = imageDao;
} }
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE || MediaType.IMAGE_PNG_VALUE) @DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> getImage(@PathVariable("id") long id) { public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData()); imageDao.delete(image.get());
return ResponseEntity.ok().contentType(image.getMediaType()).body(new InputStreamResource(inputStream)); return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} }
@DeleteMapping(value = "/images/{id}") @PostMapping(value = "/images")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) { public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
Optional<Image> image = imageDao.retrieve(id); 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);
}
try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
} catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
}
if (image.isPresent()) { @GetMapping(value = "/images", produces = "application/json")
imageDao.delete(image.get()); @ResponseBody
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK); public ArrayNode getImageList() {
} List<Image> images = imageDao.retrieveAll();
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); ArrayNode nodes = mapper.createArrayNode();
} for (Image image : images) {
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId());
objectNode.put("name", image.getName());
objectNode.put("size", image.getSize());
objectNode.put("type", image.getMediaType().toString());
nodes.add(objectNode);
}
return nodes;
}
@PostMapping(value = "/images") @GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE,
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { MediaType.IMAGE_PNG_VALUE })
public ResponseEntity<?> applyAlgorithm(@PathVariable("id") long id,
@RequestParam(required = false) String algorithm,
@RequestParam(required = false) Long p1, @RequestParam(required = false) Long p2) {
String contentType = file.getContentType(); Optional<Image> image = imageDao.retrieve(id);
if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString()) && !contentType.equals(MediaType.IMAGE_PNG.toString())) { if (image.isPresent()) {
return new ResponseEntity<>("Only JPEG/PNG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); if (algorithm == null) {
} InputStream inputStream = new ByteArrayInputStream(image.get().getData());
try { return ResponseEntity.ok().contentType(image.get().getMediaType())
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes())); .body(new InputStreamResource(inputStream));
} catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>("Image uploaded", HttpStatus.CREATED);
}
@GetMapping(value = "/images", produces = "application/json") }
@ResponseBody if (algorithm.equals("changeLuminosity")) {
public ArrayNode getImageList() { BufferedImage bufImg;
List<Image> images = imageDao.retrieveAll(); try {
ArrayNode nodes = mapper.createArrayNode(); bufImg = ImageIO.read(new ByteArrayInputStream(image.get().getData()));
for (Image image : images) { } catch (IOException e) {
ObjectNode objectNode = mapper.createObjectNode(); return new ResponseEntity<>("Image not readable",
objectNode.put("id", image.getId()); HttpStatus.INTERNAL_SERVER_ERROR);
objectNode.put("name", image.getName()); }
objectNode.put("size", image.getSize()); Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(bufImg, null,
objectNode.put("type", image.getMediaType().toString()); true, GrayU8.class);
nodes.add(objectNode); ImageProcessing.changeLuminosity(input, (int) p1.longValue());
}
return nodes;
}
@GetMapping(value = "/images/{id}?algorithm={X}&p1={Y}&p2={Z}", produces = MediaType.IMAGE_JPEG_VALUE || MediaType.IMAGE_PNG_VALUE) int width = input.getWidth();
public ResponseEntity<?> applyAlgorithm(@PathVariable("id") long id, @PathVariable("X") string algorithm, @PathVariable("Y") int firstParam, @PathVariable("Z") int secondParam) { int height = input.getHeight();
int numChannels = input.getNumBands();
Optional<Image> image = imageDao.retrieve(id); // Allocate a byte array to hold the pixel data
if (image.isPresent()) { byte[] imageData = new byte[width * height * numChannels];
if (algorithm.equals("changeLuminosity")){
Optional<Image> changedImage = createSameShape(image); // Iterate over the planes and copy the pixel data into the byte array
ImageProcessing.changeLuminosity(changedImage, firstParam); for (int i = 0; i < numChannels; i++) {
} byte[] planeData = input.getBand(i).getData();
InputStream inputStream = new ByteArrayInputStream(changedImage.get().getData()); System.arraycopy(planeData, 0, imageData, i * width * height, width *
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream)); height);
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} 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.",
HttpStatus.NOT_FOUND);
}
} }
@@ -1,10 +1,6 @@
package pdl.backend; package pdl.backend;
import java.awt.image.BufferedImage;
import boofcv.io.image.UtilImageIO;
import boofcv.io.image.ConvertBufferedImage;
import boofcv.struct.image.GrayU8; import boofcv.struct.image.GrayU8;
import boofcv.struct.border.BorderType;
import boofcv.struct.image.Planar; import boofcv.struct.image.Planar;
public class ImageProcessing { public class ImageProcessing {
@@ -13,9 +9,9 @@ public class ImageProcessing {
* *
* Retourne les valeurs r, g et b d'un pixel d'une image placés en paramètres * Retourne les valeurs r, g et b d'un pixel d'une image placés en paramètres
* *
* @param input L'image * @param input L'image
* @param x La ligne * @param x La ligne
* @param y La colonne * @param y La colonne
*/ */
public static int[] getRGBValue(Planar<GrayU8> input, int x, int y) { public static int[] getRGBValue(Planar<GrayU8> input, int x, int y) {
int[] rgb = new int[3]; int[] rgb = new int[3];
@@ -29,10 +25,10 @@ public class ImageProcessing {
* *
* Colore le pixel donné de l'image donnée avec les valeurs rgb données * Colore le pixel donné de l'image donnée avec les valeurs rgb données
* *
* @param input L'image * @param input L'image
* @param x La ligne * @param x La ligne
* @param y La colonne * @param y La colonne
* @param rgb Tableau de taille 3 avec les valeurs de r, g, et b * @param rgb Tableau de taille 3 avec les valeurs de r, g, et b
*/ */
public static void setRGBValue(Planar<GrayU8> input, int x, int y, int[] rgb) { public static void setRGBValue(Planar<GrayU8> input, int x, int y, int[] rgb) {
for (int i = 0; i < rgb.length; i++) { for (int i = 0; i < rgb.length; i++) {
@@ -127,7 +123,6 @@ public class ImageProcessing {
} }
} }
/** /**
* *
* Passe un pixel de l'espace de couleurs RGB à l'espace de couleurs HSV : * Passe un pixel de l'espace de couleurs RGB à l'espace de couleurs HSV :
@@ -249,7 +244,7 @@ public class ImageProcessing {
for (int x = 0; x < input.width; x++) { for (int x = 0; x < input.width; x++) {
if (y < half || x < half || y >= input.height - half || x >= input.width - half) { if (y < half || x < half || y >= input.height - half || x >= input.width - half) {
int gl = inputGray.getBand(0).get(x, y); int gl = inputGray.getBand(0).get(x, y);
int[] rgb = {gl,gl,gl}; int[] rgb = { gl, gl, gl };
setRGBValue(output, x, y, rgb); setRGBValue(output, x, y, rgb);
continue; continue;
} }
@@ -271,22 +266,19 @@ public class ImageProcessing {
value = 0; value = 0;
if (value > 255) if (value > 255)
value = 255; value = 255;
int[] rgb = {value, value, value}; int[] rgb = { value, value, value };
setRGBValue(output, x, y, rgb); setRGBValue(output, x, y, rgb);
} }
} }
} }
/** /**
* Applique un filtre moyenneur de taille indiquée à l'image * Applique un filtre moyenneur de taille indiquée à l'image
* passée en paramètre (le bord de l'image n'est pas modifié) * passée en paramètre (le bord de l'image n'est pas modifié)
* *
* @param input image d'entrée * @param input image d'entrée
* @param output image de sortie * @param output image de sortie
* @param size taille du filtre à appliquer * @param size taille du filtre à appliquer
*/ */
public static void meanFilter(Planar<GrayU8> input, Planar<GrayU8> output, int size) { public static void meanFilter(Planar<GrayU8> input, Planar<GrayU8> output, int size) {
// Vérification que la taille est impaire // Vérification que la taille est impaire
@@ -372,23 +364,4 @@ public class ImageProcessing {
} }
} }
} }
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("missing input or output image filename");
System.exit(-1);
}
final String inputPath = args[0];
BufferedImage image = UtilImageIO.loadImage(inputPath);
Planar<GrayU8> input = ConvertBufferedImage.convertFromPlanar(image, null, true, GrayU8.class);
Planar<GrayU8> output = input.createSameShape();
long beginning = System.nanoTime();
histogram(input);
long end = System.nanoTime();
System.out.println("Duration : " + ((end - beginning) / 1000000) + "ms");
// save output image
final String outputPath = args[1];
UtilImageIO.saveImage(input, outputPath);
System.out.println("Image saved in: " + outputPath); }
} }