Package org.apache.sanselan

Examples of org.apache.sanselan.ImageWriteException


  public void addDirectory(TiffOutputDirectory directory)
      throws ImageWriteException
  {
    if (null != findDirectory(directory.type))
      throw new ImageWriteException(
          "Output set already contains a directory of that type.");
    directories.add(directory);
  }
View Full Code Here


      throws ImageWriteException
  {
    List directories = outputSet.getDirectories();

    if (1 > directories.size())
      throw new ImageWriteException("No directories.");
   
    TiffOutputDirectory exifDirectory = null;
    TiffOutputDirectory gpsDirectory = null;
    TiffOutputDirectory interoperabilityDirectory = null;
    TiffOutputField exifDirectoryOffsetField = null;
    TiffOutputField gpsDirectoryOffsetField = null;
    TiffOutputField interoperabilityDirectoryOffsetField = null;

    ArrayList directoryIndices = new ArrayList();
    Map directoryTypeMap = new HashMap();
    for (int i = 0; i < directories.size(); i++)
    {
      TiffOutputDirectory directory = (TiffOutputDirectory) directories
          .get(i);
      int dirType = directory.type;
      Integer key = new Integer(dirType);
      directoryTypeMap.put(key, directory);
      //      Debug.debug("validating dirType", dirType + " ("
      //          + directory.getFields().size() + " fields)");

      if (dirType < 0)
      {
        switch (dirType)
        {
          case DIRECTORY_TYPE_EXIF :
            if (exifDirectory != null)
              throw new ImageWriteException(
                  "More than one EXIF directory.");
            exifDirectory = directory;
            break;

          case DIRECTORY_TYPE_GPS :
            if (gpsDirectory != null)
              throw new ImageWriteException(
                  "More than one GPS directory.");
            gpsDirectory = directory;
            break;

          case DIRECTORY_TYPE_INTEROPERABILITY :
            if (interoperabilityDirectory != null)
              throw new ImageWriteException(
                  "More than one Interoperability directory.");
            interoperabilityDirectory = directory;
            break;
          default :
            throw new ImageWriteException("Unknown directory: "
                + dirType);
        }
      }
      else
      {
        if (directoryIndices.contains(key))
          throw new ImageWriteException(
              "More than one directory with index: " + dirType
                  + ".");
        directoryIndices.add(new Integer(dirType));
        //        dirMap.put(arg0, arg1)
      }

      HashSet fieldTags = new HashSet();
      ArrayList fields = directory.getFields();
      for (int j = 0; j < fields.size(); j++)
      {
        TiffOutputField field = (TiffOutputField) fields.get(j);

        Integer fieldKey = new Integer(field.tag);
        if (fieldTags.contains(fieldKey))
          throw new ImageWriteException("Tag ("
              + field.tagInfo.getDescription()
              + ") appears twice in directory.");
        fieldTags.add(fieldKey);

        if (field.tag == EXIF_TAG_EXIF_OFFSET.tag)
        {
          if (exifDirectoryOffsetField != null)
            throw new ImageWriteException(
                "More than one Exif directory offset field.");
          exifDirectoryOffsetField = field;
        }
        else if (field.tag == EXIF_TAG_INTEROP_OFFSET.tag)
        {
          if (interoperabilityDirectoryOffsetField != null)
            throw new ImageWriteException(
                "More than one Interoperability directory offset field.");
          interoperabilityDirectoryOffsetField = field;
        }
        else if (field.tag == EXIF_TAG_GPSINFO.tag)
        {
          if (gpsDirectoryOffsetField != null)
            throw new ImageWriteException(
                "More than one GPS directory offset field.");
          gpsDirectoryOffsetField = field;
        }
      }
      //      directory.
    }

    if (directoryIndices.size() < 1)
      throw new ImageWriteException("Missing root directory.");

    // "normal" TIFF directories should have continous indices starting with 0, ie. 0, 1, 2...
    Collections.sort(directoryIndices);

    TiffOutputDirectory previousDirectory = null;
    for (int i = 0; i < directoryIndices.size(); i++)
    {
      Integer index = (Integer) directoryIndices.get(i);
      if (index.intValue() != i)
        throw new ImageWriteException("Missing directory: " + i + ".");

      // set up chain of directory references for "normal" directories.
      TiffOutputDirectory directory = (TiffOutputDirectory) directoryTypeMap
          .get(index);
      if (null != previousDirectory)
        previousDirectory.setNextDirectory(directory);
      previousDirectory = directory;
    }

    TiffOutputDirectory rootDirectory = (TiffOutputDirectory) directoryTypeMap
        .get(new Integer(DIRECTORY_TYPE_ROOT));

    // prepare results
    TiffOutputSummary result = new TiffOutputSummary(byteOrder,
        rootDirectory, directoryTypeMap);

    if (interoperabilityDirectory == null
        && interoperabilityDirectoryOffsetField != null)
    {
      // perhaps we should just discard field?
      throw new ImageWriteException(
          "Output set has Interoperability Directory Offset field, but no Interoperability Directory");
    }
    else if (interoperabilityDirectory != null)
    {
      if (exifDirectory == null)
      {
        exifDirectory = outputSet.addExifDirectory();
      }

      if (interoperabilityDirectoryOffsetField == null)
      {
        interoperabilityDirectoryOffsetField = TiffOutputField
            .createOffsetField(EXIF_TAG_INTEROP_OFFSET, byteOrder);
        exifDirectory.add(interoperabilityDirectoryOffsetField);
      }

      result.add(interoperabilityDirectory,
          interoperabilityDirectoryOffsetField);
    }

    // make sure offset fields and offset'd directories correspond.
    if (exifDirectory == null && exifDirectoryOffsetField != null)
    {
      // perhaps we should just discard field?
      throw new ImageWriteException(
          "Output set has Exif Directory Offset field, but no Exif Directory");
    }
    else if (exifDirectory != null)
    {
      if (exifDirectoryOffsetField == null)
      {
        exifDirectoryOffsetField = TiffOutputField.createOffsetField(
            EXIF_TAG_EXIF_OFFSET, byteOrder);
        rootDirectory.add(exifDirectoryOffsetField);
      }

      result.add(exifDirectory, exifDirectoryOffsetField);
    }

    if (gpsDirectory == null && gpsDirectoryOffsetField != null)
    {
      // perhaps we should just discard field?
      throw new ImageWriteException(
          "Output set has GPS Directory Offset field, but no GPS Directory");
    }
    else if (gpsDirectory != null)
    {
      if (gpsDirectoryOffsetField == null)
View Full Code Here

    {
      Object value = params.get(PARAM_KEY_COMPRESSION);
      if (value != null)
      {
        if (!(value instanceof Number))
          throw new ImageWriteException(
              "Invalid compression parameter: " + value);
        compression = ((Number) value).intValue();
      }
      params.remove(PARAM_KEY_COMPRESSION);
    }

    final int samplesPerPixel = 3; // TODO:
    final int bitsPerSample = 8; // TODO:

    //        int fRowsPerStrip; // TODO:
    int rowsPerStrip = 8000 / (width * samplesPerPixel); // TODO:
    rowsPerStrip = Math.max(1, rowsPerStrip); // must have at least one.

    byte strips[][] = getStrips(src, samplesPerPixel, bitsPerSample,
        rowsPerStrip);

    //        int stripCount = (height + fRowsPerStrip - 1) / fRowsPerStrip;
    //        int stripCount = strips.length;

    if (params.size() > 0)
    {
      Object firstKey = params.keySet().iterator().next();
      throw new ImageWriteException("Unknown parameter: " + firstKey);
    }

    //        System.out.println("width: " + width);
    //        System.out.println("height: " + height);
    //        System.out.println("fRowsPerStrip: " + fRowsPerStrip);
    //        System.out.println("fSamplesPerPixel: " + fSamplesPerPixel);
    //        System.out.println("stripCount: " + stripCount);

    if (compression == TIFF_COMPRESSION_PACKBITS)
    {
      for (int i = 0; i < strips.length; i++)
        strips[i] = new PackBits().compress(strips[i]);
    }
    else if (compression == TIFF_COMPRESSION_LZW)
    {
      for (int i = 0; i < strips.length; i++)
      {
        byte uncompressed[] = strips[i];

        int LZWMinimumCodeSize = 8;

        MyLZWCompressor compressor = new MyLZWCompressor(
            LZWMinimumCodeSize, BYTE_ORDER_MSB, true);
        byte compressed[] = compressor.compress(uncompressed);

        strips[i] = compressed;
      }
    }
    else if (compression == TIFF_COMPRESSION_UNCOMPRESSED)
    {
      // do nothing.
    }
    else
      throw new ImageWriteException(
          "Invalid compression parameter (Only LZW, Packbits and uncompressed supported).");

    TiffElement.DataElement imageData[] = new TiffElement.DataElement[strips.length];
    for (int i = 0; i < strips.length; i++)
      imageData[i] = new TiffImageData.Data(0, strips[i].length,
View Full Code Here

  protected final byte[] convertIntArrayToRationalArray(int numerators[],
      int denominators[], int byteOrder) throws ImageWriteException
  {
    if (numerators.length != denominators.length)
      throw new ImageWriteException("numerators.length ("
          + numerators.length + " != denominators.length ("
          + denominators.length + ")");

    byte result[] = new byte[numerators.length * 8];

 
View Full Code Here

      params.remove(PARAM_KEY_VERBOSE);

    if (params.size() > 0)
    {
      Object firstKey = params.keySet().iterator().next();
      throw new ImageWriteException("Unknown parameter: " + firstKey);
    }

    int width = src.getWidth();
    int height = src.getHeight();

    boolean hasAlpha = new PaletteFactory().hasTransparency(src);

    int max_colors = hasAlpha ? 255 : 256;

    Palette palette2 = new PaletteFactory().makePaletteSimple(src,
        max_colors);
    // int palette[] = new PaletteFactory().makePaletteSimple(src, 256);
    // Map palette_map = paletteToMap(palette);

    if (palette2 == null)
    {
      palette2 = new PaletteFactory().makePaletteQuantized(src,
          max_colors);
      if (verbose)
        System.out.println("quantizing");
    } else if (verbose)
      System.out.println("exact palette");

    if (palette2 == null)
      throw new ImageWriteException(
          "Gif: can't write images with more than 256 colors");
    int palette_size = palette2.length() + (hasAlpha ? 1 : 0);

    BinaryOutputStream bos = new BinaryOutputStream(os, BYTE_ORDER_LSB);
View Full Code Here

      params.remove(PARAM_KEY_PNG_FORCE_INDEXED_COLOR);
    if (params.containsKey(PARAM_KEY_PNG_BIT_DEPTH))
      params.remove(PARAM_KEY_PNG_BIT_DEPTH);
    if (params.size() > 0) {
      Object firstKey = params.keySet().iterator().next();
      throw new ImageWriteException("Unknown parameter: " + firstKey);
    }
    params = rawParams;

    int width = src.getWidth();
    int height = src.getHeight();

    boolean hasAlpha = new PaletteFactory().hasTransparency(src);
    if (verbose)
      Debug.debug("hasAlpha", hasAlpha);
    // int transparency = new PaletteFactory().getTransparency(src);

    boolean isGrayscale = new PaletteFactory().isGrayscale(src);
    if (verbose)
      Debug.debug("isGrayscale", isGrayscale);

    byte colorType;
    {
      boolean force_indexed_color = ParamMap.getParamBoolean(params,
          PARAM_KEY_PNG_FORCE_INDEXED_COLOR, false);
      boolean force_true_color = ParamMap.getParamBoolean(params,
          PARAM_KEY_PNG_FORCE_TRUE_COLOR, false);

      if (force_indexed_color && force_true_color)
        throw new ImageWriteException(
            "Params: Cannot force both indexed and true color modes");
      else if (force_indexed_color) {
        colorType = COLOR_TYPE_INDEXED_COLOR;
      } else if (force_true_color) {
        colorType = (byte) (hasAlpha ? COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
View Full Code Here

    }

    if (params.size() > 0)
    {
      Object firstKey = params.keySet().iterator().next();
      throw new ImageWriteException("Unknown parameter: " + firstKey);
    }

    int width = src.getWidth();
    int height = src.getHeight();

    boolean hasAlpha = new PaletteFactory().hasTransparency(src);

    int max_colors = hasAlpha ? 255 : 256;

    Palette palette2 = new PaletteFactory().makePaletteSimple(src,
        max_colors);
    // int palette[] = new PaletteFactory().makePaletteSimple(src, 256);
    // Map palette_map = paletteToMap(palette);

    if (palette2 == null)
    {
      palette2 = new PaletteFactory().makePaletteQuantized(src,
          max_colors);
      if (verbose)
        System.out.println("quantizing");
    } else if (verbose)
      System.out.println("exact palette");

    if (palette2 == null)
      throw new ImageWriteException(
          "Gif: can't write images with more than 256 colors");
    int palette_size = palette2.length() + (hasAlpha ? 1 : 0);

    BinaryOutputStream bos = new BinaryOutputStream(os, BYTE_ORDER_LSB);
View Full Code Here

        palette[i] = color_group.getMedianValue();

        color_group.palette_index = i;

        if (color_group.color_counts.size() < 1)
          throw new ImageWriteException("empty color_group: "
              + color_group);

        //        if(color_group.)
        //        Debug.debug("color_group", color_group);
        //        Debug.debug("palette[" + i + "]", palette[i] + " ("
        //            + Integer.toHexString(palette[i]) + ")");
      }

      if (palette_size > discrete_colors)
        throw new ImageWriteException("palette_size>discrete_colors");

      return new MedianCutPalette(root, palette);
    }
  }
View Full Code Here

        throws ImageWriteException
    {
      this.color_counts = color_counts;

      if (color_counts.size() < 1)
        throw new ImageWriteException("empty color_group");

      for (int i = 0; i < color_counts.size(); i++)
      {
        ColorCount color = (ColorCount) color_counts.get(i);
View Full Code Here

  protected void setByteOrder(int a, int b) throws ImageWriteException,
      IOException
  {
    if (a != b)
      throw new ImageWriteException("Byte Order bytes don't match (" + a
          + ", " + b + ").");

    if (a == BYTE_ORDER_MOTOROLA)
      byteOrder = a;
    else if (a == BYTE_ORDER_INTEL)
      byteOrder = a;
    else
      throw new ImageWriteException("Unknown Byte Order hint: " + a);
  }
View Full Code Here

TOP

Related Classes of org.apache.sanselan.ImageWriteException

Copyright © 2018 www.massapicom. 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.