Package gov.lanl.adore.djatoka

Examples of gov.lanl.adore.djatoka.DjatokaException


      return process(input, params);
    else if (input.getObject() != null
        && (input.getObject() instanceof InputStream))
      return process((InputStream) input.getObject(), params);
    else
      throw new DjatokaException(
          "File not defined and Input Object Type "
              + input.getObject().getClass().getName()
              + " is not supported");
  }
View Full Code Here


                raw_src.Native_destroy();

            return image;
        } catch (KduException e) {
            e.printStackTrace();
            throw new DjatokaException(e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DjatokaException(e);
        }
    }
View Full Code Here

                IOUtils.copyStream(is, fos);
                is.close();
                fos.close();
            }
        } catch (IOException e) {
            throw new DjatokaException(e);
        }

        try {
            Jp2_source inputSource = new Jp2_source();
            Kdu_compressed_source input = null;
            Jp2_family_src jp2_family_in = new Jp2_family_src();
            Jp2_locator loc = new Jp2_locator();
            jp2_family_in.Open(sourceFile, true);
            inputSource.Open(jp2_family_in, loc);
            inputSource.Read_header();
            input = inputSource;

            Kdu_codestream codestream = new Kdu_codestream();
            codestream.Create(input);
            Kdu_channel_mapping channels = new Kdu_channel_mapping();

      if (inputSource.Exists())
                channels.Configure(inputSource, false);
      else
                channels.Configure(codestream);
            int ref_component = channels.Get_source_component(0);
            Kdu_coords ref_expansion = getReferenceExpansion(ref_component, channels, codestream);
            Kdu_dims image_dims = new Kdu_dims();
            codestream.Get_dims(ref_component, image_dims);
            Kdu_coords imageSize = image_dims.Access_size();
            Kdu_coords imagePosition = image_dims.Access_pos();

      if (useleftDouble)
                left = imagePosition.Get_x() + (int) Math.round(leftDouble * imageSize.Get_x());
      if (usetopDouble)
                top = imagePosition.Get_y() + (int) Math.round(topDouble * imageSize.Get_y());
      if (useheightDouble)
                height = (int) Math.round(heightDouble * imageSize.Get_y());
      if (usewidthDouble)
                width = (int) Math.round(widthDouble * imageSize.Get_x());

            if (useRegion) {
                imageSize.Set_x(width);
                imageSize.Set_y(height);
                imagePosition.Set_x(left);
                imagePosition.Set_y(top);
            }

            int reduce = 1 << params.getLevelReductionFactor();
            imageSize.Set_x(imageSize.Get_x() * ref_expansion.Get_x());
            imageSize.Set_y(imageSize.Get_y() * ref_expansion.Get_y());
            imagePosition.Set_x(imagePosition.Get_x() * ref_expansion.Get_x() / reduce - ((ref_expansion.Get_x() / reduce - 1) / 2));
            imagePosition.Set_y(imagePosition.Get_y() * ref_expansion.Get_y() / reduce - ((ref_expansion.Get_y() / reduce - 1) / 2));

            Kdu_dims view_dims = new Kdu_dims();
            view_dims.Assign(image_dims);
            view_dims.Access_size().Set_x(imageSize.Get_x());
            view_dims.Access_size().Set_y(imageSize.Get_y());

            int region_buf_size = imageSize.Get_x() * imageSize.Get_y();
            int[] region_buf = new int[region_buf_size];
            Kdu_region_decompressor decompressor = new Kdu_region_decompressor();
            decompressor.Start(codestream, channels, -1, params.getLevelReductionFactor(), 16384, image_dims,
                    ref_expansion, new Kdu_coords(1, 1), false, Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);

            Kdu_dims new_region = new Kdu_dims();
            Kdu_dims incomplete_region = new Kdu_dims();
            Kdu_coords viewSize = view_dims.Access_size();
            incomplete_region.Assign(image_dims);

            int[] imgBuffer = new int[viewSize.Get_x() * viewSize.Get_y()];
            int[] kduBuffer = null;
            while (decompressor.Process(region_buf, image_dims.Access_pos(), 0, 0, region_buf_size, incomplete_region, new_region)) {
                Kdu_coords newOffset = new_region.Access_pos();
                Kdu_coords newSize = new_region.Access_size();
                newOffset.Subtract(view_dims.Access_pos());

                kduBuffer = region_buf;
                int imgBuffereIdx = newOffset.Get_x() + newOffset.Get_y() * viewSize.Get_x();
                int kduBufferIdx = 0;
                int xDiff = viewSize.Get_x() - newSize.Get_x();
                for (int j = 0; j < newSize.Get_y(); j++, imgBuffereIdx += xDiff) {
                    for (int i = 0; i < newSize.Get_x(); i++) {
                        imgBuffer[imgBuffereIdx++] = kduBuffer[kduBufferIdx++];
                    }
                }
            }

            BufferedImage image = new BufferedImage(imageSize.Get_x(), imageSize.Get_y(), BufferedImage.TYPE_INT_RGB);
            image.setRGB(0, 0, viewSize.Get_x(), viewSize.Get_y(), imgBuffer, 0, viewSize.Get_x());

            if (params.getRotationDegree() > 0) {
                image = ImageProcessingUtils.rotate(image, params.getRotationDegree());
            }

            decompressor.Native_destroy();
            channels.Native_destroy();
      if (codestream.Exists())
                codestream.Destroy();
            inputSource.Native_destroy();
            input.Native_destroy();
            jp2_family_in.Native_destroy();

            return image;
        } catch (KduException e) {
            e.printStackTrace();
            throw new DjatokaException(e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DjatokaException(e);
        }
    }
View Full Code Here

   * @throws DjatokaException
   */
    @Override
  public final ImageRecord getMetadata(ImageRecord r) throws DjatokaException {
    if ((r.getImageFile() == null || !new File(r.getImageFile()).exists()) && r.getObject() == null)
      throw new DjatokaException("Image Does Not Exist: " + r.toString());
    logger.debug("Get metadata: " + r.toString());
        try {
            DjatokaDecodeParam params = new DjatokaDecodeParam();
            BufferedImage bi = process(r, params);

      r.setWidth(bi.getWidth());
      r.setHeight(bi.getHeight());
            r.setDWTLevels(DEFAULT_LEVELS);
            r.setLevels(DEFAULT_LEVELS);
            r.setBitDepth(bi.getColorModel().getPixelSize());
            r.setNumChannels(bi.getColorModel().getNumColorComponents());
           
            //r.setCompositingLayerCount(getNumberOfPages(r)); // Semantics: number of pages in the PDF file.
            HashMap<String, String> pdfProps = (HashMap<String, String>)getPDFProperties(r);
            int n = Integer.parseInt(pdfProps.remove("Pages"));
            r.setCompositingLayerCount(n);
           
            // Since it is not possible for the viewer to query about a specific page's width and height
            // (because in Djatoka's point of view a PDF is just one image with various compositing layers, which are the pages),
            // at this point right here we query the PDF file about the size of all pages and store this
            // information in a Map. This map can be returned by getMetadata by setting it as the instProps member of the
            // ImageRecord class, which Djatoka already implements and which is returned as JSON to the viewer JS.
            // The viewer then has to store this information and later query it instead of asking Djatoka (getMetadata) again.
            //Map<String, String> instProps = getPagesSizes(r);
            r.setInstProps(pdfProps);
            logger.debug("instProps: " + r.getInstProps());

            logger.debug("Get metadata: "+r.toString());
    } catch (Exception e) {
      throw new DjatokaException(e);
    }

    return r;
  }
View Full Code Here


//*
    public final ImageRecord getMetadata(BufferedImage bi) throws DjatokaException {
    if (bi == null)
      throw new DjatokaException("Image Does Not Exist");

        logger.debug("getMetadata(BufferedImage): " + bi.getWidth());
        try {
            ImageRecord r = new ImageRecord();

      r.setWidth(bi.getWidth());
      r.setHeight(bi.getHeight());

            r.setDWTLevels(DEFAULT_LEVELS);
            r.setLevels(DEFAULT_LEVELS);

            r.setBitDepth(bi.getColorModel().getPixelSize());
            r.setNumChannels(bi.getColorModel().getNumColorComponents());
            //r.setCompositingLayerCount(getNumberOfPages(r)); // 'bi' refers to just one page extracted from the PDF file.
            //logger.debug("r2: "+r.toString());
           
            //TODO
           
            return r;
    } catch (Exception e) {
      throw new DjatokaException(e);
    }
  }
View Full Code Here

      throws DjatokaException {
     
    logger.debug("ExtractPDF.process:\n\tinput: " + input + "\n\tparams: " + params);
   
        if (input == null)
            throw new DjatokaException("Unknown failure while converting file: no image produced.");
           
    try {
        setPDFCommandsPath();
      } catch (IllegalStateException e) {
            logger.error("Failed to set PDF commands path: ",e);
View Full Code Here

            FileOutputStream fos = new FileOutputStream(in);
            in.deleteOnExit();
            IOUtils.copyStream(input, fos);
        } catch (IOException e) {
            logger.error(e, e);
            throw new DjatokaException(e);
        }

        BufferedImage bi = process(in.getAbsolutePath(), params);

        if (in != null) {
View Full Code Here

      return process(input.getImageFile(), params);
    else if (input.getObject() != null
        && (input.getObject() instanceof InputStream))
      return process((InputStream) input.getObject(), params);
    else
      throw new DjatokaException(
          "File not defined and Input Object Type "
              + input.getObject().getClass().getName()
              + " is not supported");
  }
View Full Code Here

                FileOutputStream fos = new FileOutputStream(in);
                IOUtils.copyStream(fis, fos);
            } catch (IOException e) {
                logger.error(e, e);
                throw new DjatokaException(e);
            }
            sourcePath = in.getAbsolutePath();
        } else {
            throw new DjatokaException(
                    "File not defined and Input Object Type "
                    + input //.getObject().getClass().getName()
                    + " is not supported");
        }
       
        String pdfinfoCmd[] = PDFINFO_COMMAND.clone();
        pdfinfoCmd[PDFINFO_COMMAND_POSITION_BIN] = pdfinfoPath;
        pdfinfoCmd[PDFINFO_COMMAND_POSITION_FIRSTPAGE] = "1";
        pdfinfoCmd[PDFINFO_COMMAND_POSITION_LASTPAGE] = "-1"; // Last page even we not knowing its number.
        pdfinfoCmd[PDFINFO_COMMAND_POSITION_FILE] = sourcePath;
        Process pdfProc = null;
        try
        {
            ArrayList<MatchResult> pageSizes = new ArrayList<MatchResult>();
            MatchResult pages = null;
           
            pdfProc = Runtime.getRuntime().exec(pdfinfoCmd);
            BufferedReader lr = new BufferedReader(new InputStreamReader(pdfProc.getInputStream()));
            String line;
            for (line = lr.readLine(); line != null; line = lr.readLine())
            {
                Matcher mm1 = PAGES_PATT.matcher(line);
                if (mm1.matches())
                    pages = mm1.toMatchResult();
                Matcher mm2 = MEDIABOX_PATT.matcher(line);
                if (mm2.matches())
                    pageSizes.add(mm2.toMatchResult());
            }

            int istatus = pdfProc.waitFor();
            if (istatus != 0)
                logger.error("pdfinfo proc failed, exit status=" + istatus + ", file=" + sourcePath);
               
            if (pages == null)
            {
                logger.error("Did not find 'Pages' line in output of pdfinfo command: " + Arrays.deepToString(pdfinfoCmd));
                pdfProperties.put("Pages", "0");
            }
            else
            {
                //int n = Integer.parseInteger(pages.group(1));
                pdfProperties.put("Pages", pages.group(1));
            }
           
            if (pageSizes.isEmpty())
            {
                logger.error("Did not find \"Page X size\" lines in output of pdfinfo command: " + Arrays.deepToString(pdfinfoCmd));
                throw new IllegalArgumentException("Failed to get pages size of PDF with pdfinfo.");
            }
            else
            {
                for (MatchResult mr : pageSizes)
                {
                    String page = mr.group(1);
                   
                    float x0 = Float.parseFloat(mr.group(2));
                    float y0 = Float.parseFloat(mr.group(3));
                    float x1 = Float.parseFloat(mr.group(4));
                    float y1 = Float.parseFloat(mr.group(5));
                    float w = Math.abs(x1 - x0);
                    float h = Math.abs(y1 - y0);
                    // Have to scale page sizes by max dpi (MAX_DPI / DEFAULT_DENSITY). Otherwise, BookReader.js will request the wrong zoom level (svc.level).
                    float ws = w * MAX_DPI / DEFAULT_DENSITY;
                    float hs = h * MAX_DPI / DEFAULT_DENSITY;
                    String width = "" + ws; //mr.group(2);
                    String height = "" + hs; //mr.group(3);
                    pdfProperties.put("Page " + page, width + " " + height);
                }
            }
           
        } catch (Exception e) {
            logger.error("Failed getting PDF information: ", e);
            throw new DjatokaException("Failed getting PDF information: ", e);
        } finally {
            // Our exec() should just consume one of the streams, but we want to stay safe.
            // http://mark.koli.ch/2011/01/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec.html
            org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getOutputStream());
            org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getInputStream());
View Full Code Here

            FileOutputStream fos = new FileOutputStream(in);
            in.deleteOnExit();
            IOUtils.copyStream(input, fos);
        } catch (IOException e) {
            logger.error(e, e);
            throw new DjatokaException(e);
        }

        BufferedImage bi = process(in.getAbsolutePath(), params);

    if (in != null)
View Full Code Here

TOP

Related Classes of gov.lanl.adore.djatoka.DjatokaException

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.