Package net.sf.jpluck.plucker

Source Code of net.sf.jpluck.plucker.ImageRecord

package net.sf.jpluck.plucker;

import net.sf.jpluck.palm.PdbOutputStream;
import net.sf.jpluck.palm.bitmap.Bitmap;
import net.sf.jpluck.util.TemporaryFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class ImageRecord extends LinkableRecord {
  public static final int BITMAP_SIZE_LIMIT = 60000;
  public static final int OPTIMAL_EMBEDDED_WIDTH = 150;
  public static final int OPTIMAL_EMBEDDED_HEIGHT = 140;

  /*private static final int SCANLINE_COMPRESSION = 0;
  private static final int NO_COMPRESSION = 0xff;
  private static final int RLE_COMPRESSION = 0x01;

  private static final int FLAG_COMPRESSED = 0x8000;
  private static final int FLAG_HAS_COLOR_TABLE = 0x4000;
  private static final int FLAG_DIRECT_COLOR = 0x0400;*/
  private Bitmap inMemoryBitmap;
  private File tempFile;

  public ImageRecord(String uri, Bitmap bitmap) {
    super(uri);

    tempFile = TemporaryFile.create("jpluck-bitmap");
    if (tempFile != null) {
      OutputStream out = null;
      try {
        out = new BufferedOutputStream(new FileOutputStream(tempFile));
        out.write(bitmap.toByteArray());
      } catch (IOException e) {
        throw new RuntimeException(e);
      } finally {
        try {
          if (out != null) {
            out.close();
          }
        } catch (IOException e2) {
        }
      }
    } else {
      // Could not create temporary file, we store the bitmap in memory
      this.inMemoryBitmap = bitmap;
    }
  }

  protected int getType() {
    return DataRecord.TBMP;
  }

  protected Paragraph[] writeData(PdbOutputStream out)
               throws IOException {
    // Restore bitmap
    Bitmap bitmap;
    if (tempFile != null) {
      InputStream in = null;
      try {
        in = new BufferedInputStream(new FileInputStream(tempFile));
        bitmap = new Bitmap(in);
      } finally {
        if (in != null) {
          in.close();
        }
        TemporaryFile.delete(tempFile);
      }
    } else {
      if (this.inMemoryBitmap == null) {
        throw new RuntimeException("Could not retrieve bitmap.");
      }
      bitmap = this.inMemoryBitmap;
    }

    int size = bitmap.getUncompressedSize();
    if (size > BITMAP_SIZE_LIMIT) {
      throw new RuntimeException("Bitmap size of " + size + " exceeds maximum size of " + BITMAP_SIZE_LIMIT);
    }
    out.write(bitmap.toByteArray());
    return null;
  }
}
TOP

Related Classes of net.sf.jpluck.plucker.ImageRecord

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.