Skip to content

Commit

Permalink
[IMG-240] Minimal parsing of symbol segments that have LUTs. (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Oct 12, 2017
1 parent 529d9d4 commit d30c9b9
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 1 deletion.
Expand Up @@ -50,6 +50,8 @@ public class SymbolSegmentParser extends AbstractSegmentParser {

private int numberOfEntriesInLUT = 0;
private int symbolExtendedSubheaderLength = 0;
static final int NUM_BYTES_IN_GRAYSCALE_LUT_ENTRY = 1;
static final int NUM_BYTES_IN_COLOUR_LUT_ENTRY = 3;

private SymbolSegmentImpl segment = null;

Expand Down Expand Up @@ -91,7 +93,7 @@ public final SymbolSegment parse(final NitfReader nitfReader, final ParseStrateg
readSROT();
readNELUT();
for (int i = 0; i < numberOfEntriesInLUT; ++i) {
throw new UnsupportedOperationException("TODO: Implement LUT parsing when we have an example");
readLUTEntry();
}
readSXSHDL();
if (symbolExtendedSubheaderLength > 0) {
Expand Down Expand Up @@ -169,6 +171,27 @@ private void readNELUT() throws NitfFormatException {
numberOfEntriesInLUT = reader.readBytesAsInteger(SYNELUT_LENGTH);
}

private void readLUTEntry() throws UnsupportedOperationException, NitfFormatException {
if (segment.getSymbolType().equals(SymbolType.BITMAP)) {
if (segment.getSymbolColour().equals(SymbolColour.USE_COLOUR_LUT)) {
readColourLUTEntry();
return;
} else if (segment.getSymbolColour().equals(SymbolColour.USE_GRAYSCALE_LUT)) {
readGrayScaleLUTEntry();
return;
}
}
throw new NitfFormatException("Symbol LUT only permitted for Symbol Type B with Symbol Colour G or C.");
}

private void readColourLUTEntry() throws NitfFormatException {
reader.skip(NUM_BYTES_IN_COLOUR_LUT_ENTRY);
}

private void readGrayScaleLUTEntry() throws NitfFormatException {
reader.skip(NUM_BYTES_IN_GRAYSCALE_LUT_ENTRY);
}

private void readSXSHDL() throws NitfFormatException {
symbolExtendedSubheaderLength = reader.readBytesAsInteger(SXSHDL_LENGTH);
}
Expand Down
@@ -0,0 +1,160 @@
/*
* Copyright (c) Codice Foundation
*
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. A copy of the GNU Lesser General Public License
* is distributed along with this program and can be found at
* <http://www.gnu.org/licenses/lgpl.html>.
*
*/
package org.codice.imaging.nitf.core.symbol.impl;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.codice.imaging.nitf.core.common.FileType;
import org.codice.imaging.nitf.core.common.NitfFormatException;
import org.codice.imaging.nitf.core.common.NitfReader;
import org.codice.imaging.nitf.core.common.impl.NitfInputStreamReader;
import org.codice.imaging.nitf.core.impl.SlottedParseStrategy;
import org.codice.imaging.nitf.core.security.SecurityClassification;
import org.codice.imaging.nitf.core.symbol.SymbolColour;
import org.codice.imaging.nitf.core.symbol.SymbolSegment;
import org.codice.imaging.nitf.core.symbol.SymbolType;
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Unit Tests for symbol segment parsing
*/
public class SymbolSegmentParserTest {

public SymbolSegmentParserTest() {
}

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void CheckCGM() throws NitfFormatException {
InputStream inputStream = new ByteArrayInputStream(("SY0000000001multi.cgm SYMBOL. U 999998This symbol will never need downgrading.0"
+ "C000000000000000100000000000000000000000 00000000000000000").getBytes(StandardCharsets.ISO_8859_1));
SlottedParseStrategy parseStrategy = new SlottedParseStrategy();
NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(inputStream));
reader.setFileType(FileType.NITF_TWO_ZERO);
SymbolSegmentParser parser = new SymbolSegmentParser();
SymbolSegment symbolSegment = parser.parse(reader, parseStrategy, 0);
assertEquals("0000000001", symbolSegment.getIdentifier());
assertEquals("multi.cgm SYMBOL.", symbolSegment.getSymbolName());
assertEquals(SecurityClassification.UNCLASSIFIED, symbolSegment.getSecurityMetadata().getSecurityClassification());
assertEquals(SymbolType.CGM, symbolSegment.getSymbolType());
assertEquals(0, symbolSegment.getNumberOfLinesPerSymbol());
assertEquals(0, symbolSegment.getNumberOfPixelsPerLine());
assertEquals(0, symbolSegment.getLineWidth());
assertEquals(0, symbolSegment.getNumberOfBitsPerPixel());
assertEquals(1, symbolSegment.getSymbolDisplayLevel());
assertEquals(0, symbolSegment.getAttachmentLevel());
assertEquals(0, symbolSegment.getSymbolLocationRow());
assertEquals(0, symbolSegment.getSymbolLocationColumn());
assertEquals(0, symbolSegment.getSymbolLocation2Row());
assertEquals(0, symbolSegment.getSymbolLocation2Column());
assertEquals(SymbolColour.NOT_APPLICABLE, symbolSegment.getSymbolColour());
assertEquals("000000", symbolSegment.getSymbolNumber());
assertEquals(0, symbolSegment.getSymbolRotation());
assertEquals(0, symbolSegment.getExtendedHeaderDataOverflow());
}

@Test
public void CheckBitmapGrayscaleLUT() throws NitfFormatException {
InputStream inputStream = new ByteArrayInputStream(("SY0000000021abcdefghijklmnopqrstU 999998This symbol will never need downgrading.0"
+ "B001800350000100900800025000350000000000G000000145002pf00003002").getBytes(StandardCharsets.ISO_8859_1));
SlottedParseStrategy parseStrategy = new SlottedParseStrategy();
NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(inputStream));
reader.setFileType(FileType.NITF_TWO_ZERO);
SymbolSegmentParser parser = new SymbolSegmentParser();
SymbolSegment symbolSegment = parser.parse(reader, parseStrategy, 0);
assertEquals("0000000021", symbolSegment.getIdentifier());
assertEquals("abcdefghijklmnopqrst", symbolSegment.getSymbolName());
assertEquals(SecurityClassification.UNCLASSIFIED, symbolSegment.getSecurityMetadata().getSecurityClassification());
assertEquals(SymbolType.BITMAP, symbolSegment.getSymbolType());
assertEquals(18, symbolSegment.getNumberOfLinesPerSymbol());
assertEquals(35, symbolSegment.getNumberOfPixelsPerLine());
assertEquals(0, symbolSegment.getLineWidth());
assertEquals(1, symbolSegment.getNumberOfBitsPerPixel());
assertEquals(9, symbolSegment.getSymbolDisplayLevel());
assertEquals(8, symbolSegment.getAttachmentLevel());
assertEquals(25, symbolSegment.getSymbolLocationRow());
assertEquals(35, symbolSegment.getSymbolLocationColumn());
assertEquals(0, symbolSegment.getSymbolLocation2Row());
assertEquals(0, symbolSegment.getSymbolLocation2Column());
assertEquals(SymbolColour.USE_GRAYSCALE_LUT, symbolSegment.getSymbolColour());
assertEquals("000000", symbolSegment.getSymbolNumber());
assertEquals(145, symbolSegment.getSymbolRotation());
assertEquals(2, symbolSegment.getExtendedHeaderDataOverflow());
}

@Test
public void CheckBitmapColourLUT() throws NitfFormatException {
InputStream inputStream = new ByteArrayInputStream(("SY0000000021abcdefghijklmnopqrstU 999998This symbol will never need downgrading.0"
+ "B001800350000100900800025000350000000000C000000145002pfgAHC00003002").getBytes(StandardCharsets.ISO_8859_1));
SlottedParseStrategy parseStrategy = new SlottedParseStrategy();
NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(inputStream));
reader.setFileType(FileType.NITF_TWO_ZERO);
SymbolSegmentParser parser = new SymbolSegmentParser();
SymbolSegment symbolSegment = parser.parse(reader, parseStrategy, 0);
assertEquals("0000000021", symbolSegment.getIdentifier());
assertEquals("abcdefghijklmnopqrst", symbolSegment.getSymbolName());
assertEquals(SecurityClassification.UNCLASSIFIED, symbolSegment.getSecurityMetadata().getSecurityClassification());
assertEquals(SymbolType.BITMAP, symbolSegment.getSymbolType());
assertEquals(18, symbolSegment.getNumberOfLinesPerSymbol());
assertEquals(35, symbolSegment.getNumberOfPixelsPerLine());
assertEquals(0, symbolSegment.getLineWidth());
assertEquals(1, symbolSegment.getNumberOfBitsPerPixel());
assertEquals(9, symbolSegment.getSymbolDisplayLevel());
assertEquals(8, symbolSegment.getAttachmentLevel());
assertEquals(25, symbolSegment.getSymbolLocationRow());
assertEquals(35, symbolSegment.getSymbolLocationColumn());
assertEquals(0, symbolSegment.getSymbolLocation2Row());
assertEquals(0, symbolSegment.getSymbolLocation2Column());
assertEquals(SymbolColour.USE_COLOUR_LUT, symbolSegment.getSymbolColour());
assertEquals("000000", symbolSegment.getSymbolNumber());
assertEquals(145, symbolSegment.getSymbolRotation());
assertEquals(2, symbolSegment.getExtendedHeaderDataOverflow());
}

@Test
public void CheckBadSymbolTypeLUT() throws NitfFormatException {
InputStream inputStream = new ByteArrayInputStream(("SY0000000021abcdefghijklmnopqrstU 999998This symbol will never need downgrading.0"
+ "O001800350000100900800025000350000000000C000000145002pfgAHC00003002").getBytes(StandardCharsets.ISO_8859_1));
SlottedParseStrategy parseStrategy = new SlottedParseStrategy();
NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(inputStream));
reader.setFileType(FileType.NITF_TWO_ZERO);
SymbolSegmentParser parser = new SymbolSegmentParser();

exception.expect(NitfFormatException.class);
exception.expectMessage("Symbol LUT only permitted for Symbol Type B with Symbol Colour G or C.");
parser.parse(reader, parseStrategy, 0);
}

@Test
public void CheckBadSymbolColourLUT() throws NitfFormatException {
InputStream inputStream = new ByteArrayInputStream(("SY0000000021abcdefghijklmnopqrstU 999998This symbol will never need downgrading.0"
+ "B001800350000100900800025000350000000000K000000145002pfgAHC00003002").getBytes(StandardCharsets.ISO_8859_1));
SlottedParseStrategy parseStrategy = new SlottedParseStrategy();
NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(inputStream));
reader.setFileType(FileType.NITF_TWO_ZERO);
SymbolSegmentParser parser = new SymbolSegmentParser();

exception.expect(NitfFormatException.class);
exception.expectMessage("Symbol LUT only permitted for Symbol Type B with Symbol Colour G or C.");
parser.parse(reader, parseStrategy, 0);
}
}

0 comments on commit d30c9b9

Please sign in to comment.