ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296
ByteArrayOutputStream baos =new ByteArrayOutputStream();
OutputStream os =baos;
JPEGOutputStream jpegos =null;
if(comp==JPEG){
jpegos=new JPEGOutputStream(baos);
int quality=(param==null)?50:(int)(param.getCompressionQuality()*100);
jpegos.setZZQuantizationTable(0,JPEGConstants.LQT,quality);
jpegos.setZZQuantizationTable(1,JPEGConstants.CQT,quality);
jpegos.setRawDCHuffmanTable(0,JPEGConstants.HLDCTable);
jpegos.setRawACHuffmanTable(0,JPEGConstants.HLACTable);
jpegos.setRawDCHuffmanTable(1,JPEGConstants.HCDCTable);
jpegos.setRawACHuffmanTable(1,JPEGConstants.HCACTable);
jpegos.defineQuantizationTables();
jpegos.defineHuffmanTables();
jpegos.close();
DEFactory.JPEGTablesDE jpegtables=new DEFactory.JPEGTablesDE(baos.toByteArray());
ifd.add(jpegtables); // 347
baos.reset();
os=jpegos;
}
// CCIR Recommendation 601-1 LumaRed=299/1000 LumaGreen=587/1000 LumeBlue=114/1000
// Y = ( LumaRed * R + LumaGreen * G + LumaBlue * B )
// Cb = ( B - Y ) / ( 2 - 2 * LumaBlue )
// Cr = ( R - Y ) / ( 2 - 2 * LumaRed )
double LumaRed = 299.0/1000.0;
double LumaGreen = 587.0/1000.0;
double LumaBlue = 114.0/1000.0;
DEFactory.YCbCrCoefficientsDE YCbCrCoeff = new DEFactory.YCbCrCoefficientsDE();
YCbCrCoeff.setLumaRed (LumaRed); // Y
YCbCrCoeff.setLumaGreen(LumaGreen); // Cb
YCbCrCoeff.setLumaBlue (LumaBlue); // Cr
ifd.add(YCbCrCoeff); // 529
DEFactory.YCbCrSubSamplingDE YCbCrSubSampling = new DEFactory.YCbCrSubSamplingDE();
YCbCrSubSampling.setHoriz(ssh);
YCbCrSubSampling.setVert(ssv);
ifd.add(YCbCrSubSampling); // 530
double RfBY =0; double RfWY =255;
double RfBCb =128; double RfWCb =255;
double RfBCr =128; double RfWCr =255;
DEFactory.ReferenceBlackWhiteDE ReferenceBlackWhite = new DEFactory.ReferenceBlackWhiteDE();
ReferenceBlackWhite.setY(RfBY,RfWY);
ReferenceBlackWhite.setCb(RfBCb,RfWCb);
ReferenceBlackWhite.setCr(RfBCr,RfWCr);
ifd.add(ReferenceBlackWhite); // 532
TIFFYCbCrOutputStream ycbcros;
if(jpegos==null){
ycbcros=new TIFFYCbCrOutputStream(os,width,ssv,ssh);
os=new TIFFSubSamplingOutputStream(ycbcros,width,ssv,ssh);
}else{
ycbcros=new TIFFYCbCrOutputStream(os,width,1,1); // jpeg does own subsampling
os=ycbcros;
}
ycbcros.setPositioning(1);
ycbcros.setColourCoefficients(LumaRed,LumaGreen,LumaBlue);
ycbcros.setRfBWY (RfBY,RfWY);
ycbcros.setRfBWCb(RfBCb,RfWCb);
ycbcros.setRfBWCr(RfBCr,RfWCr);
WritableRaster raster=image.getRaster();
DataBufferInt buffer=(DataBufferInt)raster.getDataBuffer();
int[] imgdata=(int[])buffer.getData();
int c=0,index=0;
for(int y=0;y<height;y+=maxrps){
if((height-y)<maxrps){maxrps=height-y;}
if(jpegos!=null){
jpegos.startOfImage();
int[] hv={(ssh<<4)|ssv,0x11,0x11}; // (Hi<<4)|Vi
int[] q={0,1,1}; // quantization table Y=0, Cb=Cr=1
// jpegos.startOfFrame(((maxrps+ssv-1)/ssv)*ssv,ww,hv,q);
jpegos.startOfFrame(maxrps,ww,hv,q);
int[] sel={0,1,1}; // DC,AC code table Y=0, Cb=Cr=1
jpegos.startOfScan(sel);
}
for(int i=0;i<maxrps;i++){
int x=0;
while(x<width){