Search in sources :

Example 1 with NitfFormatException

use of org.codice.imaging.nitf.core.common.NitfFormatException in project alliance by codice.

the class CatalogOutputAdapterTest method testGetNitfBinaryContentBlockedChip.

@Test
public void testGetNitfBinaryContentBlockedChip() throws MimeTypeParseException, NitfFormatException, IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    // 200000N0200000E 200000N0400000E
    // 000000N0200000E 000000N0400000E
    int originalWidth = 2048;
    int originalHeight = 2048;
    int chipX = 0;
    int chipY = 0;
    int chipWidth = 2048;
    int chipHeight = 2048;
    BufferedImage chipImage = new BufferedImage(chipWidth, chipHeight, BufferedImage.TYPE_3BYTE_BGR);
    FileSecurityMetadata fileSecurityMetadata = createFileSecurityMetadata();
    DateTime dateTime = DateTimeImpl.getNitfDateTimeForNow();
    DataSource dataSource = mock(DataSource.class);
    NitfHeader nitfHeader = getNitfHeader(fileSecurityMetadata, dateTime);
    ImageCoordinates imageCoordinates = getImageCoordinates();
    SecurityMetadata imageSecurityMetadata = getImageSecurityMetadata();
    ImageSegment imageSegment = getImageSegment(originalWidth, originalHeight, dateTime, imageCoordinates, imageSecurityMetadata);
    when(dataSource.getNitfHeader()).thenReturn(nitfHeader);
    when(dataSource.getImageSegments()).thenReturn(Collections.singletonList(imageSegment));
    Constructor<NitfSegmentsFlowImpl> constructor;
    constructor = NitfSegmentsFlowImpl.class.getDeclaredConstructor(DataSource.class, Runnable.class);
    constructor.setAccessible(true);
    NitfSegmentsFlow nitfSegmentsFlow = constructor.newInstance(dataSource, (Runnable) () -> {
    });
    BinaryContent binaryContent = catalogOutputAdapter.getNitfBinaryContent(chipImage, nitfSegmentsFlow, chipX, chipY);
    NitfSegmentsFlow chipNitfSegmentFlow = new NitfParserInputFlowImpl().inputStream(binaryContent.getInputStream()).allData();
    assertThat(chipNitfSegmentFlow, notNullValue());
    chipNitfSegmentFlow.fileHeader(nh -> {
        assertThat(nh.getFileType(), is(FileType.NITF_TWO_ONE));
        assertThat(nh.getFileTitle(), is("FileTitle"));
        assertThat(nh.getStandardType(), is("BF01"));
        assertThat(nh.getOriginatingStationId(), is("U21SOO90"));
        assertThat(nh.getFileBackgroundColour().getRed(), is((byte) 0));
        assertThat(nh.getFileBackgroundColour().getGreen(), is((byte) 0));
        assertThat(nh.getFileBackgroundColour().getBlue(), is((byte) 0));
        assertThat(nh.getFileDateTime(), notNullValue());
        assertThat(nh.getOriginatorsName(), is("W.TEMPEL"));
        assertThat(nh.getOriginatorsPhoneNumber(), is("44 1480 84 5611"));
        assertThat(nh.getFileSecurityMetadata().getSecurityClassification(), is(SecurityClassification.UNCLASSIFIED));
        assertThat(nh.getFileSecurityMetadata().getSecurityClassificationSystem(), is("AB"));
        assertThat(nh.getFileSecurityMetadata().getCodewords(), is("ABCDEFGHIJK"));
        assertThat(nh.getFileSecurityMetadata().getControlAndHandling(), is("AB"));
        assertThat(nh.getFileSecurityMetadata().getReleaseInstructions(), is("01234567890123456789"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationType(), is("DD"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationDate(), is("20160101"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationExemption(), is("abcd"));
        assertThat(nh.getFileSecurityMetadata().getDowngrade(), is("S"));
        assertThat(nh.getFileSecurityMetadata().getDowngradeDate(), is("20160202"));
        assertThat(nh.getFileSecurityMetadata().getClassificationText(), is("classtext"));
        assertThat(nh.getFileSecurityMetadata().getClassificationAuthorityType(), is("O"));
        assertThat(nh.getFileSecurityMetadata().getClassificationAuthority(), is("MyAuthority"));
        assertThat(nh.getFileSecurityMetadata().getClassificationReason(), is("A"));
        assertThat(nh.getFileSecurityMetadata().getSecuritySourceDate(), is("20160303"));
        assertThat(nh.getFileSecurityMetadata().getSecurityControlNumber(), is("012345678901234"));
        assertThat(nh.getFileSecurityMetadata().getFileCopyNumber(), is("99999"));
        assertThat(nh.getFileSecurityMetadata().getFileNumberOfCopies(), is("99999"));
    });
    chipNitfSegmentFlow.forEachImageSegment(imageSegment1 -> {
        assertThat(imageSegment1.getImageCoordinates().getCoordinate00().getLongitude(), closeTo(20, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate00().getLatitude(), closeTo(20, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate0MaxCol().getLongitude(), closeTo(40, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate0MaxCol().getLatitude(), closeTo(20, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRowMaxCol().getLongitude(), closeTo(40, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRowMaxCol().getLatitude(), closeTo(0, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRow0().getLongitude(), closeTo(20, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRow0().getLatitude(), closeTo(0, 0.01));
        assertThat(imageSegment1.getNumberOfColumns(), is((long) chipWidth));
        assertThat(imageSegment1.getNumberOfRows(), is((long) chipHeight));
        assertThat(imageSegment1.getImageCategory(), is(ImageCategory.UNKNOWN));
        assertThat(imageSegment1.getImageComments(), is(Collections.singletonList("MyComment")));
        assertThat(imageSegment1.getIdentifier(), is("0123456789"));
        assertThat(imageSegment1.getImageIdentifier2(), is("abc"));
        assertThat(imageSegment1.getImageMagnification(), is("1.0 "));
        try {
            assertThat(imageSegment1.getImageTargetId().textValue(), is(new TargetIdImpl("                 ").textValue()));
        } catch (NitfFormatException e) {
            fail(e.getMessage());
        }
        assertThat(imageSegment1.getImageSource(), is(""));
        assertThat(imageSegment1.getImageDateTime().getSourceString(), is(dateTime.getSourceString()));
        assertThat(imageSegment1.getImageRepresentation(), is(ImageRepresentation.RGBTRUECOLOUR));
    });
}
Also used : ImageSegment(org.codice.imaging.nitf.core.image.ImageSegment) TargetIdImpl(org.codice.imaging.nitf.core.image.impl.TargetIdImpl) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) NitfSegmentsFlow(org.codice.imaging.nitf.fluent.NitfSegmentsFlow) NitfParserInputFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfParserInputFlowImpl) BinaryContent(ddf.catalog.data.BinaryContent) FileSecurityMetadata(org.codice.imaging.nitf.core.security.FileSecurityMetadata) BufferedImage(java.awt.image.BufferedImage) DateTime(org.codice.imaging.nitf.core.common.DateTime) DataSource(org.codice.imaging.nitf.core.DataSource) NitfSegmentsFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfSegmentsFlowImpl) NitfHeader(org.codice.imaging.nitf.core.header.NitfHeader) ImageCoordinates(org.codice.imaging.nitf.core.image.ImageCoordinates) SecurityMetadata(org.codice.imaging.nitf.core.security.SecurityMetadata) FileSecurityMetadata(org.codice.imaging.nitf.core.security.FileSecurityMetadata) Test(org.junit.Test)

Example 2 with NitfFormatException

use of org.codice.imaging.nitf.core.common.NitfFormatException in project alliance by codice.

the class CatalogOutputAdapterTest method testGetNitfSegmentsFlowTFBOSAsBytesourceThrows.

@SuppressWarnings("unchecked")
@Test
public void testGetNitfSegmentsFlowTFBOSAsBytesourceThrows() throws IOException, NitfFormatException {
    ByteSource byteSource = mock(ByteSource.class);
    when(byteSource.openBufferedStream()).thenThrow(IOException.class);
    TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
    when(tfbos.asByteSource()).thenReturn(byteSource);
    catalogOutputAdapter = new CatalogOutputAdapter() {

        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
            return tfbos;
        }
    };
    try {
        catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] { (byte) 0 }));
        fail("expected an exception, shouldn't reach this line");
    } catch (IOException | NitfFormatException e) {
        assertThat(e, notNullValue());
    }
    verify(tfbos).close();
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with NitfFormatException

use of org.codice.imaging.nitf.core.common.NitfFormatException in project alliance by codice.

the class NitfPreStoragePlugin method process.

private void process(ContentItem contentItem, List<ContentItem> contentItems) {
    Metacard metacard = contentItem.getMetacard();
    if (!isNitfMimeType(contentItem.getMimeTypeRawData())) {
        LOGGER.debug("skipping content item: filename={} mimeType={}", contentItem.getFilename(), contentItem.getMimeTypeRawData());
        return;
    }
    try {
        if (contentItem.getSize() / MEGABYTE > maxNitfSizeMB) {
            LOGGER.debug("Skipping large ({} MB) content item: filename={}", contentItem.getSize() / MEGABYTE, contentItem.getFilename());
            return;
        }
        BufferedImage renderedImage = renderImage(contentItem);
        if (renderedImage != null) {
            addThumbnailToMetacard(metacard, renderedImage);
            if (createOverview) {
                ContentItem overviewContentItem = createDerivedImage(contentItem.getId(), OVERVIEW, renderedImage, metacard, calculateOverviewWidth(renderedImage), calculateOverviewHeight(renderedImage));
                contentItems.add(overviewContentItem);
            }
            if (storeOriginalImage) {
                ContentItem originalImageContentItem = createOriginalImage(contentItem.getId(), renderImageUsingOriginalDataModel(contentItem), metacard);
                contentItems.add(originalImageContentItem);
            }
        }
    } catch (IOException | ParseException | NitfFormatException | RuntimeException e) {
        LOGGER.debug(e.getMessage(), e);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) IOException(java.io.IOException) ParseException(java.text.ParseException) MimeTypeParseException(javax.activation.MimeTypeParseException) BufferedImage(java.awt.image.BufferedImage) ContentItem(ddf.catalog.content.data.ContentItem)

Example 4 with NitfFormatException

use of org.codice.imaging.nitf.core.common.NitfFormatException in project alliance by codice.

the class CatalogOutputAdapterTest method testGetNitfBinaryContent.

@Test
public void testGetNitfBinaryContent() throws MimeTypeParseException, NitfFormatException, IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    // 200000N0200000E 200000N0400000E
    // 000000N0200000E 000000N0400000E
    int originalWidth = 200;
    int originalHeight = 100;
    int chipX = 20;
    int chipY = 10;
    int chipWidth = 160;
    int chipHeight = 80;
    // assume original image is 200x100, chip is 20x10/160x80
    BufferedImage chipImage = new BufferedImage(chipWidth, chipHeight, BufferedImage.TYPE_3BYTE_BGR);
    FileSecurityMetadata fileSecurityMetadata = createFileSecurityMetadata();
    DateTime dateTime = DateTimeImpl.getNitfDateTimeForNow();
    NitfHeader nitfHeader = getNitfHeader(fileSecurityMetadata, dateTime);
    ImageCoordinates imageCoordinates = getImageCoordinates();
    SecurityMetadata imageSecurityMetadata = getImageSecurityMetadata();
    ImageSegment imageSegment = getImageSegment(originalWidth, originalHeight, dateTime, imageCoordinates, imageSecurityMetadata);
    DataSource dataSource = getDataSource(nitfHeader, Collections.singletonList(imageSegment));
    Constructor<NitfSegmentsFlowImpl> constructor;
    constructor = NitfSegmentsFlowImpl.class.getDeclaredConstructor(DataSource.class, Runnable.class);
    constructor.setAccessible(true);
    NitfSegmentsFlow nitfSegmentsFlow = constructor.newInstance(dataSource, (Runnable) () -> {
    });
    BinaryContent binaryContent = catalogOutputAdapter.getNitfBinaryContent(chipImage, nitfSegmentsFlow, chipX, chipY);
    NitfSegmentsFlow chipNitfSegmentFlow = new NitfParserInputFlowImpl().inputStream(binaryContent.getInputStream()).allData();
    assertThat(chipNitfSegmentFlow, notNullValue());
    chipNitfSegmentFlow.fileHeader(nh -> {
        assertThat(nh.getFileType(), is(FileType.NITF_TWO_ONE));
        assertThat(nh.getFileTitle(), is("FileTitle"));
        assertThat(nh.getStandardType(), is("BF01"));
        assertThat(nh.getOriginatingStationId(), is("U21SOO90"));
        assertThat(nh.getFileBackgroundColour().getRed(), is((byte) 0));
        assertThat(nh.getFileBackgroundColour().getGreen(), is((byte) 0));
        assertThat(nh.getFileBackgroundColour().getBlue(), is((byte) 0));
        assertThat(nh.getFileDateTime(), notNullValue());
        assertThat(nh.getOriginatorsName(), is("W.TEMPEL"));
        assertThat(nh.getOriginatorsPhoneNumber(), is("44 1480 84 5611"));
        assertThat(nh.getFileSecurityMetadata().getSecurityClassification(), is(SecurityClassification.UNCLASSIFIED));
        assertThat(nh.getFileSecurityMetadata().getSecurityClassificationSystem(), is("AB"));
        assertThat(nh.getFileSecurityMetadata().getCodewords(), is("ABCDEFGHIJK"));
        assertThat(nh.getFileSecurityMetadata().getControlAndHandling(), is("AB"));
        assertThat(nh.getFileSecurityMetadata().getReleaseInstructions(), is("01234567890123456789"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationType(), is("DD"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationDate(), is("20160101"));
        assertThat(nh.getFileSecurityMetadata().getDeclassificationExemption(), is("abcd"));
        assertThat(nh.getFileSecurityMetadata().getDowngrade(), is("S"));
        assertThat(nh.getFileSecurityMetadata().getDowngradeDate(), is("20160202"));
        assertThat(nh.getFileSecurityMetadata().getClassificationText(), is("classtext"));
        assertThat(nh.getFileSecurityMetadata().getClassificationAuthorityType(), is("O"));
        assertThat(nh.getFileSecurityMetadata().getClassificationAuthority(), is("MyAuthority"));
        assertThat(nh.getFileSecurityMetadata().getClassificationReason(), is("A"));
        assertThat(nh.getFileSecurityMetadata().getSecuritySourceDate(), is("20160303"));
        assertThat(nh.getFileSecurityMetadata().getSecurityControlNumber(), is("012345678901234"));
        assertThat(nh.getFileSecurityMetadata().getFileCopyNumber(), is("99999"));
        assertThat(nh.getFileSecurityMetadata().getFileNumberOfCopies(), is("99999"));
    });
    chipNitfSegmentFlow.forEachImageSegment(imageSegment1 -> {
        assertThat(imageSegment1.getImageCoordinates().getCoordinate00().getLongitude(), closeTo(22, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate00().getLatitude(), closeTo(18, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate0MaxCol().getLongitude(), closeTo(38, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinate0MaxCol().getLatitude(), closeTo(18, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRowMaxCol().getLongitude(), closeTo(38, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRowMaxCol().getLatitude(), closeTo(2, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRow0().getLongitude(), closeTo(22, 0.01));
        assertThat(imageSegment1.getImageCoordinates().getCoordinateMaxRow0().getLatitude(), closeTo(2, 0.01));
        assertThat(imageSegment1.getNumberOfColumns(), is((long) chipWidth));
        assertThat(imageSegment1.getNumberOfRows(), is((long) chipHeight));
        assertThat(imageSegment1.getImageCategory(), is(ImageCategory.UNKNOWN));
        assertThat(imageSegment1.getImageComments(), is(Collections.singletonList("MyComment")));
        assertThat(imageSegment1.getIdentifier(), is("0123456789"));
        assertThat(imageSegment1.getImageIdentifier2(), is("abc"));
        assertThat(imageSegment1.getImageMagnification(), is("1.0 "));
        try {
            assertThat(imageSegment1.getImageTargetId().textValue(), is(new TargetIdImpl("                 ").textValue()));
        } catch (NitfFormatException e) {
            fail(e.getMessage());
        }
        assertThat(imageSegment1.getImageSource(), is(""));
        assertThat(imageSegment1.getImageDateTime().getSourceString(), is(dateTime.getSourceString()));
        assertThat(imageSegment1.getImageRepresentation(), is(ImageRepresentation.RGBTRUECOLOUR));
    });
}
Also used : ImageSegment(org.codice.imaging.nitf.core.image.ImageSegment) TargetIdImpl(org.codice.imaging.nitf.core.image.impl.TargetIdImpl) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) NitfSegmentsFlow(org.codice.imaging.nitf.fluent.NitfSegmentsFlow) NitfParserInputFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfParserInputFlowImpl) BinaryContent(ddf.catalog.data.BinaryContent) FileSecurityMetadata(org.codice.imaging.nitf.core.security.FileSecurityMetadata) BufferedImage(java.awt.image.BufferedImage) DateTime(org.codice.imaging.nitf.core.common.DateTime) DataSource(org.codice.imaging.nitf.core.DataSource) NitfSegmentsFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfSegmentsFlowImpl) NitfHeader(org.codice.imaging.nitf.core.header.NitfHeader) ImageCoordinates(org.codice.imaging.nitf.core.image.ImageCoordinates) SecurityMetadata(org.codice.imaging.nitf.core.security.SecurityMetadata) FileSecurityMetadata(org.codice.imaging.nitf.core.security.FileSecurityMetadata) Test(org.junit.Test)

Example 5 with NitfFormatException

use of org.codice.imaging.nitf.core.common.NitfFormatException in project alliance by codice.

the class CatalogOutputAdapterTest method testGetNitfSegmentsFlowTFBOSThrowsDuringRead.

@SuppressWarnings("unchecked")
@Test
public void testGetNitfSegmentsFlowTFBOSThrowsDuringRead() throws IOException, NitfFormatException {
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read()).thenThrow(IOException.class);
    when(inputStream.read(anyObject())).thenThrow(IOException.class);
    when(inputStream.read(anyObject(), anyInt(), anyInt())).thenThrow(IOException.class);
    ByteSource byteSource = mock(ByteSource.class);
    when(byteSource.openBufferedStream()).thenReturn(inputStream);
    TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
    when(tfbos.asByteSource()).thenReturn(byteSource);
    catalogOutputAdapter = new CatalogOutputAdapter() {

        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
            return tfbos;
        }
    };
    try {
        catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] { (byte) 0 }));
        fail("expected an exception, shouldn't reach this line");
    } catch (IOException | NitfFormatException e) {
        assertThat(e, notNullValue());
    }
    verify(tfbos).close();
    verify(inputStream).close();
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

NitfFormatException (org.codice.imaging.nitf.core.common.NitfFormatException)6 BufferedImage (java.awt.image.BufferedImage)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ByteSource (com.google.common.io.ByteSource)3 ImageSegment (org.codice.imaging.nitf.core.image.ImageSegment)3 NitfParserInputFlowImpl (org.codice.imaging.nitf.fluent.impl.NitfParserInputFlowImpl)3 ContentItem (ddf.catalog.content.data.ContentItem)2 BinaryContent (ddf.catalog.data.BinaryContent)2 Metacard (ddf.catalog.data.Metacard)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ParseException (java.text.ParseException)2 MimeTypeParseException (javax.activation.MimeTypeParseException)2 J2KImageWriteParam (com.github.jaiimageio.jpeg2000.J2KImageWriteParam)1 J2KImageReaderSpi (com.github.jaiimageio.jpeg2000.impl.J2KImageReaderSpi)1 J2KImageWriter (com.github.jaiimageio.jpeg2000.impl.J2KImageWriter)1 J2KImageWriterSpi (com.github.jaiimageio.jpeg2000.impl.J2KImageWriterSpi)1 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)1 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)1