Package org.apache.sanselan

Examples of org.apache.sanselan.ImageWriteException


  private void writeChunkzTXt(OutputStream os, PngText.zTXt text)
      throws IOException, ImageWriteException
  {
    if (!UnicodeUtils.isValidISO_8859_1(text.keyword))
      throw new ImageWriteException(
          "Png zTXt chunk keyword is not ISO-8859-1: " + text.keyword);
    if (!UnicodeUtils.isValidISO_8859_1(text.text))
      throw new ImageWriteException(
          "Png zTXt chunk text is not ISO-8859-1: " + text.text);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // keyword
View Full Code Here


  private void writeChunktEXt(OutputStream os, PngText.tEXt text)
      throws IOException, ImageWriteException
  {
    if (!UnicodeUtils.isValidISO_8859_1(text.keyword))
      throw new ImageWriteException(
          "Png tEXt chunk keyword is not ISO-8859-1: " + text.keyword);
    if (!UnicodeUtils.isValidISO_8859_1(text.text))
      throw new ImageWriteException(
          "Png tEXt chunk text is not ISO-8859-1: " + text.text);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // keyword
View Full Code Here

    if (params.containsKey(PARAM_KEY_PNG_TEXT_CHUNKS))
      params.remove(PARAM_KEY_PNG_TEXT_CHUNKS);
    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 forceIndexedColor = ParamMap.getParamBoolean(params,
          PARAM_KEY_PNG_FORCE_INDEXED_COLOR, false);
      boolean forceTrueColor = ParamMap.getParamBoolean(params,
          PARAM_KEY_PNG_FORCE_TRUE_COLOR, false);

      if (forceIndexedColor && forceTrueColor)
        throw new ImageWriteException(
            "Params: Cannot force both indexed and true color modes");
      else if (forceIndexedColor)
      {
        colorType = COLOR_TYPE_INDEXED_COLOR;
      } else if (forceTrueColor)
      {
        colorType = (byte) (hasAlpha ? COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
            : COLOR_TYPE_TRUE_COLOR);
      } else
        colorType = getColourType(hasAlpha, isGrayscale);
      if (verbose)
        Debug.debug("colorType", colorType);
    }

    byte bitDepth = getBitDepth(colorType, params);
    if (verbose)
      Debug.debug("bit_depth", bitDepth);

    int sampleDepth;
    if (colorType == COLOR_TYPE_INDEXED_COLOR)
      sampleDepth = 8;
    else
      sampleDepth = bitDepth;
    if (verbose)
      Debug.debug("sample_depth", sampleDepth);

    {
      os.write(PNG_Signature);
    }
    {
      // IHDR must be first

      byte compressionMethod = COMPRESSION_TYPE_INFLATE_DEFLATE;
      byte filterMethod = FILTER_METHOD_ADAPTIVE;
      byte interlaceMethod = INTERLACE_METHOD_NONE;

      ImageHeader imageHeader = new ImageHeader(width, height, bitDepth,
          colorType, compressionMethod, filterMethod, interlaceMethod);

      writeChunkIHDR(os, imageHeader);
    }

    {
      // sRGB No Before PLTE and IDAT. If the sRGB chunk is present, the
      // iCCP chunk should not be present.

      // charles
    }

    Palette palette = null;
    if (colorType == COLOR_TYPE_INDEXED_COLOR)
    {
      // PLTE No Before first IDAT

      int max_colors = hasAlpha ? 255 : 256;

      palette = new MedianCutQuantizer(true).process(src, max_colors,
          verbose);
      // Palette palette2 = new PaletteFactory().makePaletteSimple(src,
      // max_colors);

      // palette.dump();

      writeChunkPLTE(os, palette);
    }

    if (params.containsKey(PARAM_KEY_XMP_XML))
    {
      String xmpXml = (String) params.get(PARAM_KEY_XMP_XML);
      writeChunkXmpiTXt(os, xmpXml);
    }

    if (params.containsKey(PARAM_KEY_PNG_TEXT_CHUNKS))
    {
      List outputTexts = (List) params.get(PARAM_KEY_PNG_TEXT_CHUNKS);
      for (int i = 0; i < outputTexts.size(); i++)
      {
        PngText text = (PngText) outputTexts.get(i);
        if (text instanceof PngText.tEXt)
          writeChunktEXt(os, (PngText.tEXt) text);
        else if (text instanceof PngText.zTXt)
          writeChunkzTXt(os, (PngText.zTXt) text);
        else if (text instanceof PngText.iTXt)
          writeChunkiTXt(os, (PngText.iTXt) text);
        else
          throw new ImageWriteException(
              "Unknown text to embed in PNG: " + text);
      }
    }

    {
View Full Code Here

            .getRationalNumber(number);
      }
      return convertRationalArrayToByteArray(rationalNumbers, byteOrder);
    }
    else
      throw new ImageWriteException("Invalid data: " + o + " ("
          + Debug.getType(o) + ")");
  }
View Full Code Here

  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

        return dstDir;
      }
      catch (ImageReadException e)
      {
        throw new ImageWriteException(e.getMessage(), e);
      }
    }
View Full Code Here

      return result;
    }
    catch (ImageReadException e)
    {
      throw new ImageWriteException(e.getMessage(), e);
    }
  }
View Full Code Here

      throws IOException, ImageWriteException
  {
    List analysis = analyzeOldTiff();
    int oldLength = exifBytes.length;
    if (analysis.size() < 1)
      throw new ImageWriteException("Couldn't analyze old tiff data.");
    else if (analysis.size() == 1)
    {
      TiffElement onlyElement = (TiffElement) analysis.get(0);
      //      Debug.debug("onlyElement", onlyElement.getElementDescription());
      if (onlyElement.offset == TIFF_HEADER_SIZE
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 LZW_MINIMUM_CODE_SIZE = 8;

        MyLZWCompressor compressor = new MyLZWCompressor(
            LZW_MINIMUM_CODE_SIZE, 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

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.