|
| 1 | +package org.jmisb.examples.rangeimagegenerator; |
| 2 | + |
| 3 | +import java.awt.image.BufferedImage; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.SortedMap; |
| 8 | +import java.util.TreeMap; |
| 9 | +import java.util.UUID; |
| 10 | +import javax.imageio.ImageIO; |
| 11 | +import org.jmisb.api.common.KlvParseException; |
| 12 | +import org.jmisb.api.klv.st1204.CoreIdentifier; |
| 13 | +import org.jmisb.api.video.CodecIdentifier; |
| 14 | +import org.jmisb.api.video.IVideoFileOutput; |
| 15 | +import org.jmisb.api.video.KlvFormat; |
| 16 | +import org.jmisb.api.video.MetadataFrame; |
| 17 | +import org.jmisb.api.video.VideoFileOutput; |
| 18 | +import org.jmisb.api.video.VideoFrame; |
| 19 | +import org.jmisb.api.video.VideoOutputOptions; |
| 20 | +import org.jmisb.core.video.TimingUtils; |
| 21 | +import org.jmisb.st1002.GeneralizedTransformation; |
| 22 | +import org.jmisb.st1002.IRangeImageMetadataValue; |
| 23 | +import org.jmisb.st1002.RangeImageCompressionMethod; |
| 24 | +import org.jmisb.st1002.RangeImageEnumerations; |
| 25 | +import org.jmisb.st1002.RangeImageLocalSet; |
| 26 | +import org.jmisb.st1002.RangeImageMetadataKey; |
| 27 | +import org.jmisb.st1002.RangeImageSource; |
| 28 | +import org.jmisb.st1002.RangeImageryDataType; |
| 29 | +import org.jmisb.st1002.ST1002PrecisionTimeStamp; |
| 30 | +import org.jmisb.st1002.ST1002VersionNumber; |
| 31 | +import org.jmisb.st1002.SinglePointRangeMeasurement; |
| 32 | +import org.jmisb.st1002.SinglePointRangeMeasurementColumn; |
| 33 | +import org.jmisb.st1002.SinglePointRangeMeasurementRow; |
| 34 | +import org.jmisb.st1010.SDCC; |
| 35 | +import org.jmisb.st1202.Denominator_X; |
| 36 | +import org.jmisb.st1202.Denominator_Y; |
| 37 | +import org.jmisb.st1202.GeneralizedTransformationLocalSet; |
| 38 | +import org.jmisb.st1202.GeneralizedTransformationParametersKey; |
| 39 | +import org.jmisb.st1202.IGeneralizedTransformationMetadataValue; |
| 40 | +import org.jmisb.st1202.SDCC_FLP; |
| 41 | +import org.jmisb.st1202.ST1202DocumentVersion; |
| 42 | +import org.jmisb.st1202.TransformationEnumeration; |
| 43 | +import org.jmisb.st1202.X_Numerator_Constant; |
| 44 | +import org.jmisb.st1202.X_Numerator_X; |
| 45 | +import org.jmisb.st1202.X_Numerator_Y; |
| 46 | +import org.jmisb.st1202.Y_Numerator_Constant; |
| 47 | +import org.jmisb.st1202.Y_Numerator_X; |
| 48 | +import org.jmisb.st1202.Y_Numerator_Y; |
| 49 | +import org.slf4j.Logger; |
| 50 | +import org.slf4j.LoggerFactory; |
| 51 | + |
| 52 | +public class Generator { |
| 53 | + |
| 54 | + private static Logger LOG = LoggerFactory.getLogger(Generator.class); |
| 55 | + private final int width = 1280; |
| 56 | + private final int height = 960; |
| 57 | + private final int bitRate = 500_000; |
| 58 | + private final int gopSize = 30; |
| 59 | + private final double frameRate = 15.0; |
| 60 | + private final double frameDuration = 1.0 / frameRate; |
| 61 | + private final int duration = 60; |
| 62 | + private KlvFormat klvFormat = KlvFormat.Synchronous; |
| 63 | + private CodecIdentifier codec = CodecIdentifier.H264; |
| 64 | + private String filename = "rangeimage.ts"; |
| 65 | + |
| 66 | + public Generator() throws KlvParseException {} |
| 67 | + |
| 68 | + public void setKlvFormat(KlvFormat klvFormat) { |
| 69 | + this.klvFormat = klvFormat; |
| 70 | + } |
| 71 | + |
| 72 | + public void setCodec(CodecIdentifier codec) { |
| 73 | + this.codec = codec; |
| 74 | + } |
| 75 | + |
| 76 | + public void setOutputFile(String filename) { |
| 77 | + this.filename = filename; |
| 78 | + } |
| 79 | + |
| 80 | + public void generate() throws KlvParseException { |
| 81 | + showConfiguration(); |
| 82 | + CoreIdentifier coreIdentifier = new CoreIdentifier(); |
| 83 | + coreIdentifier.setMinorUUID(UUID.randomUUID()); |
| 84 | + coreIdentifier.setVersion(1); |
| 85 | + |
| 86 | + try (IVideoFileOutput output = |
| 87 | + new VideoFileOutput( |
| 88 | + new VideoOutputOptions( |
| 89 | + width, height, bitRate, frameRate, gopSize, klvFormat, codec))) { |
| 90 | + output.open(filename); |
| 91 | + |
| 92 | + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); |
| 93 | + try { |
| 94 | + image = ImageIO.read(new File("test1280.jpg")); |
| 95 | + } catch (IOException e) { |
| 96 | + // TODO: log |
| 97 | + } |
| 98 | + |
| 99 | + final long numFrames = duration * Math.round(frameRate); |
| 100 | + long startTime = System.currentTimeMillis(); |
| 101 | + double pts = 1000.0 * System.currentTimeMillis(); // Close enough for this. |
| 102 | + for (long i = 0; i < numFrames; ++i) { |
| 103 | + output.addVideoFrame(new VideoFrame(image, pts * 1.0e-6)); |
| 104 | + SortedMap<RangeImageMetadataKey, IRangeImageMetadataValue> values = new TreeMap<>(); |
| 105 | + values.put( |
| 106 | + RangeImageMetadataKey.PrecisionTimeStamp, |
| 107 | + new ST1002PrecisionTimeStamp((long) pts)); |
| 108 | + values.put(RangeImageMetadataKey.DocumentVersion, new ST1002VersionNumber(2)); |
| 109 | + values.put( |
| 110 | + RangeImageMetadataKey.RangeImageEnumerations, |
| 111 | + new RangeImageEnumerations( |
| 112 | + RangeImageCompressionMethod.NO_COMPRESSION, |
| 113 | + RangeImageryDataType.PERSPECTIVE, |
| 114 | + RangeImageSource.RANGE_SENSOR)); |
| 115 | + values.put( |
| 116 | + RangeImageMetadataKey.SinglePointRangeMeasurement, |
| 117 | + new SinglePointRangeMeasurement(8000)); |
| 118 | + values.put( |
| 119 | + RangeImageMetadataKey.SinglePointRangeMeasurementRowCoordinate, |
| 120 | + new SinglePointRangeMeasurementRow(403)); |
| 121 | + values.put( |
| 122 | + RangeImageMetadataKey.SinglePointRangeMeasurementColumnCoordinate, |
| 123 | + new SinglePointRangeMeasurementColumn(803)); |
| 124 | + Map<GeneralizedTransformationParametersKey, IGeneralizedTransformationMetadataValue> |
| 125 | + st1202values = new TreeMap<>(); |
| 126 | + st1202values.put( |
| 127 | + GeneralizedTransformationParametersKey.X_Numerator_x, new X_Numerator_X(0)); |
| 128 | + st1202values.put( |
| 129 | + GeneralizedTransformationParametersKey.X_Numerator_y, new X_Numerator_Y(0)); |
| 130 | + st1202values.put( |
| 131 | + GeneralizedTransformationParametersKey.X_Numerator_Constant, |
| 132 | + new X_Numerator_Constant(0)); |
| 133 | + st1202values.put( |
| 134 | + GeneralizedTransformationParametersKey.Y_Numerator_x, new Y_Numerator_X(0)); |
| 135 | + st1202values.put( |
| 136 | + GeneralizedTransformationParametersKey.Y_Numerator_y, new Y_Numerator_Y(0)); |
| 137 | + st1202values.put( |
| 138 | + GeneralizedTransformationParametersKey.Y_Numerator_Constant, |
| 139 | + new Y_Numerator_Constant(0)); |
| 140 | + st1202values.put( |
| 141 | + GeneralizedTransformationParametersKey.Denominator_x, new Denominator_X(0)); |
| 142 | + st1202values.put( |
| 143 | + GeneralizedTransformationParametersKey.Denominator_y, new Denominator_Y(0)); |
| 144 | + st1202values.put( |
| 145 | + GeneralizedTransformationParametersKey.DocumentVersion, |
| 146 | + new ST1202DocumentVersion(2)); |
| 147 | + SDCC sdcc = new SDCC(); |
| 148 | + sdcc.setValues( |
| 149 | + new double[][] { |
| 150 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 151 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 152 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 153 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 154 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 155 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 156 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 157 | + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} |
| 158 | + }); |
| 159 | + st1202values.put(GeneralizedTransformationParametersKey.SDCC, new SDCC_FLP(sdcc)); |
| 160 | + st1202values.put( |
| 161 | + GeneralizedTransformationParametersKey.TransformationEnumeration, |
| 162 | + TransformationEnumeration.CHILD_PARENT); |
| 163 | + values.put( |
| 164 | + RangeImageMetadataKey.GeneralizedTransformationLocalSet, |
| 165 | + new GeneralizedTransformation( |
| 166 | + new GeneralizedTransformationLocalSet(st1202values))); |
| 167 | + RangeImageLocalSet localSet = new RangeImageLocalSet(values); |
| 168 | + output.addMetadataFrame(new MetadataFrame(localSet, pts)); |
| 169 | + pts += frameDuration * 1.0e6; |
| 170 | + long elapsedTime = System.currentTimeMillis() - startTime; |
| 171 | + long requiredElapsedTime = (long) ((i + 1) * frameDuration * 1000.0); |
| 172 | + long waitTime = requiredElapsedTime - elapsedTime; |
| 173 | + if (waitTime > 0) { |
| 174 | + TimingUtils.shortWait(waitTime); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + } catch (IOException e) { |
| 179 | + LOG.error("Failed to write file", e); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private void showConfiguration() { |
| 184 | + System.out.println("Generating with configuration:"); |
| 185 | + System.out.println(toString()); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public String toString() { |
| 190 | + return "Generator{" |
| 191 | + + "width=" |
| 192 | + + width |
| 193 | + + ", height=" |
| 194 | + + height |
| 195 | + + ", bitRate=" |
| 196 | + + bitRate |
| 197 | + + ", gopSize=" |
| 198 | + + gopSize |
| 199 | + + ", frameRate=" |
| 200 | + + frameRate |
| 201 | + + ", frameDuration=" |
| 202 | + + frameDuration |
| 203 | + + ", duration=" |
| 204 | + + duration |
| 205 | + + ",\nklvFormat=" |
| 206 | + + klvFormat |
| 207 | + + ",\nfilename=" |
| 208 | + + filename |
| 209 | + + '}'; |
| 210 | + } |
| 211 | +} |
0 commit comments