2015-04-11 23:52:45 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
public class JPEG extends Const
|
|
|
|
{
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/** MEMBERS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* location of the source file
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private String filename;
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* byte array containing the actual file
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private byte[] rawData;
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* byte array containing EXIF block information without header
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private byte[] exif;
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* byte array containing StandardXMP block information without header
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private byte[] xmpSta;
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* byte array containing ExtendedXMP block information without header
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private byte[] xmpExt;
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* byte array conatining the trailing headless data block conatining the blurred jpeg image
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private byte[] tail;
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/** METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param rawData
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public JPEG(byte[] rawData)
|
|
|
|
{
|
|
|
|
this.init(rawData, null);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Constructor, that reads a byte array from file
|
|
|
|
* @param filename
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public JPEG(String filename)
|
|
|
|
{
|
|
|
|
this.init(IO.read(new File(filename)), filename);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Disassemble the rawData byte array into sub arrays
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void disassemble()
|
|
|
|
{
|
|
|
|
exif = this.getEXIFBlock();
|
|
|
|
xmpSta = this.getStandardXMPBlockContent();
|
|
|
|
xmpExt = this.getExtendedXMPContent();
|
|
|
|
tail = this.getImageTail();
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* extract the depthmap from the jpeg and store it in the same location as the jpeg itself but with the suffix _d.png
|
|
|
|
* @return
|
|
|
|
*/
|
2015-04-12 00:28:15 +02:00
|
|
|
public boolean exportDepthMap()
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
|
|
|
String out = filename;
|
|
|
|
if(out.endsWith(".jpg") || out.endsWith(".JPG")) out = out.substring(0, out.length()-4);
|
2015-04-12 00:28:15 +02:00
|
|
|
return this.exportDepthMap(out+"_d.png");
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* extract the depthmap from the jpeg and store it under the location specified in "file"
|
|
|
|
* @param file String of the output location
|
|
|
|
* @return success
|
|
|
|
*/
|
2015-04-12 00:28:15 +02:00
|
|
|
public boolean exportDepthMap(String file)
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
|
|
|
byte[] b64 = ArrayUtils.unsign(extractDepthMap());
|
|
|
|
byte[] depth = Base64.getDecoder().decode(b64);
|
2015-04-12 00:28:15 +02:00
|
|
|
if(depth != null)
|
|
|
|
{
|
|
|
|
IO.write(depth, file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extract and save the unblurred source image. The image will be saved in the same location as the jpeg itself, but with the suffix "_s.jpg"
|
|
|
|
* @return success
|
|
|
|
*/
|
2015-04-12 00:28:15 +02:00
|
|
|
public boolean exportSourceImage()
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
|
|
|
String out = filename;
|
|
|
|
if(out.endsWith(".jpg") || out.endsWith(".JPG")) out = out.substring(0, out.length()-4);
|
2015-04-12 00:28:15 +02:00
|
|
|
return this.exportSourceImage(out+"_s.jpg");
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extract the unblurred source image and save it under the location specified in "file"
|
|
|
|
* @param file String of the location
|
|
|
|
* @return success
|
|
|
|
*/
|
2015-04-12 00:28:15 +02:00
|
|
|
public boolean exportSourceImage(String file)
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
|
|
|
byte[] b64 = ArrayUtils.unsign(extractSourceImage());
|
|
|
|
byte[] src = Base64.getDecoder().decode(b64);
|
2015-04-12 00:28:15 +02:00
|
|
|
if(src != null)
|
|
|
|
{
|
|
|
|
IO.write(src, file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extract and return the depthmap information
|
|
|
|
* @return depthmap as byte array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] extractDepthMap()
|
|
|
|
{
|
|
|
|
return JPEGUtils.extractDepthMap(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extract and return the unblurred source image
|
|
|
|
* @return source image as byte array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] extractSourceImage()
|
|
|
|
{
|
|
|
|
return JPEGUtils.extractSourceImage(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Find all indizes of APP1 markers in the jpegs byte array
|
|
|
|
* @return integer array of all positions of the APP1 marker in the rawData array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public int[] getBoundaries()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getBoundaries(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* return the exif block of the jpeg as a byte array
|
|
|
|
* @return exifblock (member)
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getExif()
|
|
|
|
{
|
|
|
|
return exif;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Gets the exif block from the rawData array instead from the member
|
|
|
|
* @return the exif block from rawData
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getEXIFBlock()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getEXIFBlock(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extract and return the content of all the ExtendedXMP blocks concatenated.
|
|
|
|
* @return content of the ExtendedXMP blocks
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getExtendedXMPContent()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getExtendedXMPBlockContent(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* return the filename and path
|
|
|
|
* @return
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public String getFilename()
|
|
|
|
{
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Return the headless block of data in the image (this contains the JPEG you see when opening the file)
|
|
|
|
* @return headless JPEG tail
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getImageTail()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getImageTail(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Return the Metadata of the image (content of all APP1 marked blocks)
|
|
|
|
* @return metadata
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getMetadata()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getMetadata(rawData);
|
|
|
|
}
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the byte array that IO.read(image) returned
|
|
|
|
* @return byte array of the image file
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getRawData()
|
|
|
|
{
|
|
|
|
return this.rawData;
|
|
|
|
}
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts and returns the content of the StandardXMP block
|
|
|
|
* @return content of the StandardXMP block
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getStandardXMPBlockContent()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getStandardXMPBlockContent(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Returns the Image tail (member)
|
|
|
|
* @return member image tail
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getTail()
|
|
|
|
{
|
|
|
|
return tail;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Extracts and returns the content of all XMP blocks concatenated (StandardXMP + ExtendedXMPs)
|
|
|
|
* @return content of the XMP blocks
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getXMPBlocksContent()
|
|
|
|
{
|
|
|
|
return JPEGUtils.getXMPBlocksContent(rawData);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Return the member containing the ExtendedXMP information
|
|
|
|
* @return XmpExt (member)
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getXmpExt()
|
|
|
|
{
|
|
|
|
return xmpExt;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Return the member containing the StandardXMP information
|
|
|
|
* @return xmpSta (member)
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] getXmpSta()
|
|
|
|
{
|
|
|
|
return xmpSta;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* initializes the JPEG object.
|
|
|
|
* set the array containing the image as member rawData, set filename
|
|
|
|
* disassemble the Image and set the other members with content.
|
|
|
|
* @param raw byte array of the image
|
|
|
|
* @param filename location
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
private void init(byte[] raw, String filename)
|
|
|
|
{
|
|
|
|
this.rawData = raw;
|
|
|
|
this.filename = filename;
|
|
|
|
this.disassemble();
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Inject a new depthmap into the jpeg ("replace" the old GDepth:Data value with the Base64 encoded png file)
|
|
|
|
* @param filename location of the new depthmap png
|
|
|
|
* @return success
|
|
|
|
*/
|
2015-04-12 00:28:15 +02:00
|
|
|
public boolean injectDepthMap(String filename)
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
|
|
|
byte[] depth = Base64.getEncoder().encode(IO.read(new File(filename)));
|
|
|
|
xmpExt = JPEGUtils.replace(xmpExt, keyGDepthData, depth);
|
2015-04-12 00:28:15 +02:00
|
|
|
if(xmpExt != null) return true;
|
|
|
|
else return false;
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Reassemble a functional jpeg byte array from block data.
|
|
|
|
* Set rawData to the new array and also return it
|
|
|
|
* @return reassembled jpeg byte array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public byte[] reassemble()
|
|
|
|
{
|
|
|
|
byte[] md5 = HexUtil.generateMD5(xmpExt);
|
|
|
|
|
|
|
|
byte[] out = markJPG;
|
|
|
|
out = ArrayUtils.concatenate(out, JPEGUtils.decorateBlock(exif,EXIF));
|
|
|
|
out = ArrayUtils.concatenate(out, JPEGUtils.decorateBlock(xmpSta, STANDARDXMP));
|
|
|
|
out = ArrayUtils.concatenate(out, JPEGUtils.decorateBlock(xmpExt, EXTENDEDXMP));
|
|
|
|
out = ArrayUtils.concatenate(out, tail);
|
|
|
|
out = JPEGUtils.replace(out, keyHasExtendedXMP, md5);
|
|
|
|
this.rawData = out;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Write the image to disk (location is defined in filename)
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void save()
|
|
|
|
{
|
|
|
|
this.save(filename);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Write the image to the file specified in "file"
|
|
|
|
* @param file
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void save(String file)
|
|
|
|
{
|
|
|
|
IO.write(this.reassemble(), file);
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Set the exif memberto a new byte array
|
|
|
|
* @param exif new array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void setExif(byte[] exif)
|
|
|
|
{
|
|
|
|
this.exif=exif;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Set the tail member array to a new byte array
|
|
|
|
* @param tail new array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void setTail(byte[] tail)
|
|
|
|
{
|
|
|
|
this.tail=tail;
|
|
|
|
}
|
2015-04-13 22:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set the xmpExt member array to a new byte array
|
|
|
|
* @param xmpExt new array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void setXmpExt(byte[] xmpExt)
|
|
|
|
{
|
|
|
|
this.xmpExt=xmpExt;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* set the xmpSta member array to a new byte array
|
|
|
|
* @param xmpSta new array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public void setXmpSta(byte[] xmpSta)
|
|
|
|
{
|
|
|
|
this.xmpSta=xmpSta;
|
|
|
|
}
|
|
|
|
}
|