Skip to content

Commit

Permalink
Bug fix for loading images after a Split and Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
hbitteur committed Dec 5, 2024
1 parent 76dab8c commit 490dc44
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 60 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/org/audiveris/omr/image/ImageLoading.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ private static Loader getPdfLoader (Path imgPath)
public static Loader getLoader (Path imgPath)
{
// Avoid stupid errors
if (imgPath == null) {
logger.warn("Null file path", imgPath);

return null;
}

if (!Files.exists(imgPath)) {
logger.warn("File {} does not exist", imgPath);

Expand Down
52 changes: 6 additions & 46 deletions app/src/main/java/org/audiveris/omr/sheet/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -166,6 +165,7 @@ public class Book
* <p>
* The path was valid at book creation and recorded in the book .omr project file.
* It may not be relevant when the same .omr file is used on another machine.
* NOTA: It is null for a compound book.
*/
@XmlAttribute(name = "path")
@XmlJavaTypeAdapter(Jaxb.PathAdapter.class)
Expand Down Expand Up @@ -409,8 +409,8 @@ private void beforeMarshal (Marshaller m)
// checkRadixChange //
//------------------//
/**
* If the (new) book name does not match current one, update the book radix
* (and the title of first displayed sheet if any).
* If the (new) book name does not match the current one, update the book radix
* (and the title of the first displayed sheet if any).
*
* @param bookPath new book target path
*/
Expand Down Expand Up @@ -554,7 +554,7 @@ private void createScores (List<SheetStub> validSelectedStubs,
*/
public void createStubs ()
{
ImageLoading.Loader loader = ImageLoading.getLoader(path);
final ImageLoading.Loader loader = ImageLoading.getLoader(path);

if (loader != null) {
final int imageCount = loader.getImageCount();
Expand Down Expand Up @@ -1780,48 +1780,6 @@ public boolean isUpgraded ()
return bookUpgraded;
}

//----------------//
// loadSheetImage //
//----------------//
/**
* Actually load the image that corresponds to the specified sheet id.
*
* @param id specified sheet id
* @return the loaded sheet image
*/
public synchronized BufferedImage loadSheetImage (int id)
{
try {
if (!Files.exists(path)) {
logger.info("Book input {} not found", path);

return null;
}

final ImageLoading.Loader loader = ImageLoading.getLoader(path);

if (loader == null) {
return null;
}

final BufferedImage img = loader.getImage(id);
logger.info(
"Loaded image #{} {}x{} from {}",
id,
img.getWidth(),
img.getHeight(),
path);

loader.dispose();

return img;
} catch (IOException ex) {
logger.warn("Error in book.loadSheetImage", ex);

return null;
}
}

//------------------//
// migrateOldParams //
//------------------//
Expand Down Expand Up @@ -2063,6 +2021,8 @@ public boolean reachBookStep (final OmrStep target,
LogUtil.stopStub();
}
}

logger.info("Book processed.");
}

return !someFailure;
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/org/audiveris/omr/sheet/Picture.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,8 @@ public BufferedImage getGrayImage ()

if (gray == null) {
try {
// Try to reload image from book input path
SheetStub stub = sheet.getStub();
gray = stub.getBook().loadSheetImage(stub.getNumber());
// Try to reload image from sheet input information
gray = sheet.getStub().loadGrayImage();

if (gray == null) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/audiveris/omr/sheet/Sheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1018,12 +1018,12 @@ public AtomicInteger getPersistentIdGenerator ()
public Picture getPicture ()
{
if (picture == null) {
BufferedImage img = getBook().loadSheetImage(stub.getNumber());
final BufferedImage img = stub.loadGrayImage();

try {
setImage(img, true);
} catch (StepException ex) {
logger.warn("Error setting image id {}", stub.getNumber(), ex);
logger.warn("Error setting image for sheet {}", stub.getNumber(), ex);
}
}

Expand Down
48 changes: 46 additions & 2 deletions app/src/main/java/org/audiveris/omr/sheet/SheetStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.audiveris.omr.constant.ConstantSet;
import org.audiveris.omr.image.FilterDescriptor;
import org.audiveris.omr.image.FilterParam;
import org.audiveris.omr.image.ImageLoading;
import org.audiveris.omr.log.LogUtil;
import org.audiveris.omr.run.RunTable;
import org.audiveris.omr.score.Page;
Expand Down Expand Up @@ -1238,6 +1239,49 @@ public boolean isValid ()
return !invalid;
}

//---------------//
// loadGrayImage //
//---------------//
/**
* Actually load the image that corresponds to this sheet stub.
*
* @return the loaded sheet image
*/
public synchronized BufferedImage loadGrayImage ()
{
try {
final SheetInput si = getSheetInput();

if (!Files.exists(si.path)) {
logger.info("Input {} not found", si.path);

return null;
}

final ImageLoading.Loader loader = ImageLoading.getLoader(si.path);

if (loader == null) {
return null;
}

final BufferedImage img = loader.getImage(si.number);
logger.info(
"Loaded image #{} {}x{} from {}",
si.number,
img.getWidth(),
img.getHeight(),
si.path);

loader.dispose();

return img;
} catch (IOException ex) {
logger.warn("Error in SheetStub.loadGrayImage", ex);

return null;
}
}

//------------------//
// migrateOldParams //
//------------------//
Expand Down Expand Up @@ -1395,7 +1439,7 @@ public void reset ()
doReset();

try {
BufferedImage img = book.loadSheetImage(number);
BufferedImage img = loadGrayImage();
sheet = new Sheet(this, img, true);
logger.info("Sheet#{} reset as valid.", number);
display();
Expand Down Expand Up @@ -1437,7 +1481,7 @@ public void resetToBinary ()
public void resetToGray ()
{
try {
final BufferedImage img = book.loadSheetImage(number);
final BufferedImage img = loadGrayImage();

if (img != null) {
doReset();
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/audiveris/omr/sheet/ui/BookActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1130,9 +1130,9 @@ public Task<Void, Void> resetBookToGray (ActionEvent e)
final Book book = StubsController.getCurrentBook();

if (book != null) {
final Path inputPath = book.getInputPath();
final Path inputPath = book.getInputPath(); // Null for a compound book

if (!Files.exists(inputPath)) {
if ((inputPath != null) && !Files.exists(inputPath)) {
OMR.gui.displayWarning("Cannot find " + inputPath, "Source images not available");
return null;
}
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/org/audiveris/omr/step/LoadStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.audiveris.omr.constant.Constant;
import org.audiveris.omr.constant.ConstantSet;
import org.audiveris.omr.sheet.Book;
import org.audiveris.omr.sheet.Sheet;
import org.audiveris.omr.sheet.SheetStub;
import org.audiveris.omr.sheet.ui.SheetTab;
Expand Down Expand Up @@ -67,10 +66,7 @@ public void doit (Sheet sheet)
throws StepException
{
final SheetStub stub = sheet.getStub();
final Book book = stub.getBook();
final int number = stub.getNumber();

final BufferedImage image = book.loadSheetImage(number);
final BufferedImage image = stub.loadGrayImage();

if (image != null) {
// Threshold on image size
Expand Down

0 comments on commit 490dc44

Please sign in to comment.