Search in sources :

Example 1 with BitmapRef

use of org.ebookdroid.common.bitmaps.BitmapRef in project LibreraReader by foobnix.

the class DecodeServiceBase method checkCropping.

RectF checkCropping(final DecodeTask task, final CodecPage vuPage) {
    // Checks if cropping setting is not set
    if (task.viewState.book == null || !task.viewState.book.cropPages) {
        return null;
    }
    // Checks if page has been cropped before
    if (task.node.croppedBounds != null) {
        // Page size is actuall now
        return null;
    }
    RectF croppedPageBounds = null;
    // Checks if page root node has been cropped before
    final PageTreeNode root = task.node.page.nodes.root;
    if (root.croppedBounds == null) {
        final Rect rootRect = new Rect(0, 0, Math.min(PageCropper.MAX_WIDTH, vuPage.getWidth()), Math.min(PageCropper.MAX_HEIGHT, vuPage.getHeight()));
        // final Rect rootRect = new Rect(0, 0, rootRect.width(), rootRect.height());
        // TempHolder.lock.lock();
        LOG.d("DJVU1-1", rootRect.width(), rootRect.height());
        final BitmapRef rootBitmap = vuPage.renderBitmapSimple(rootRect.width(), rootRect.height(), new RectF(0, 0, 1f, 1f));
        // TempHolder.lock.unlock();
        LOG.d("DJVU1-1a", vuPage.getWidth(), vuPage.getHeight());
        LOG.d("DJVU1-1b", rootBitmap.getBitmap().getWidth(), rootBitmap.getBitmap().getHeight());
        root.croppedBounds = PageCropper.getCropBounds(rootBitmap.getBitmap(), rootRect, new RectF(0, 0, 1f, 1f));
        LOG.d("DJVU1-2", root.croppedBounds.width(), root.croppedBounds.height());
        BitmapManager.release(rootBitmap);
        final ViewState viewState = task.viewState;
        final float pageWidth = vuPage.getWidth() * root.croppedBounds.width();
        final float pageHeight = vuPage.getHeight() * root.croppedBounds.height();
        LOG.d("DJVU1-3", pageWidth, pageHeight);
        final PageIndex currentPage = viewState.book.getCurrentPage();
        final float offsetX = viewState.book.offsetX;
        final float offsetY = viewState.book.offsetY;
        root.page.setAspectRatio(pageWidth, pageHeight);
        viewState.ctrl.invalidatePageSizes(InvalidateSizeReason.PAGE_LOADED, task.node.page);
        croppedPageBounds = root.page.getBounds(task.viewState.zoom);
        task.node.page.base.getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                viewState.ctrl.goToPage(currentPage.viewIndex, offsetX, offsetY);
            }
        });
    }
    if (task.node != root) {
        task.node.croppedBounds = PageTreeNode.evaluateCroppedPageSliceBounds(task.node.pageSliceBounds, task.node.parent);
    }
    return croppedPageBounds;
}
Also used : RectF(android.graphics.RectF) Rect(android.graphics.Rect) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef)

Example 2 with BitmapRef

use of org.ebookdroid.common.bitmaps.BitmapRef in project LibreraReader by foobnix.

the class DjvuPage method renderBitmapSimple.

@Override
public BitmapRef renderBitmapSimple(int width, int height, RectF pageSliceBounds) {
    LOG.d("Render DJVU Page", width, height, pageSliceBounds);
    final int renderMode = AppSettings.getInstance().djvuRenderingMode;
    BitmapRef bmp = null;
    if (width > 0 && height > 0) {
        bmp = BitmapManager.getBitmap("Djvu page", width, height, Bitmap.Config.RGB_565);
        final int[] buffer = new int[width * height];
        renderPageWrapper(pageHandle, contextHandle, width, height, pageSliceBounds.left, pageSliceBounds.top, pageSliceBounds.width(), pageSliceBounds.height(), buffer, renderMode);
        bmp.getBitmap().setPixels(buffer, 0, width, 0, 0, width, height);
        return bmp;
    }
    if (bmp == null) {
        bmp = BitmapManager.getBitmap("Djvu page", 100, 100, Bitmap.Config.RGB_565);
    }
    return bmp;
}
Also used : BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef)

Example 3 with BitmapRef

use of org.ebookdroid.common.bitmaps.BitmapRef in project LibreraReader by foobnix.

the class MuPdfPage method renderThumbnail.

@Override
public Bitmap renderThumbnail(final int width, final int originW, final int originH) {
    final RectF rectF = new RectF(0, 0, 1f, 1f);
    final float k = (float) originH / originW;
    LOG.d("TEST", "Render" + " w" + getWidth() + " H " + getHeight() + " " + k + " " + width * k);
    final BitmapRef renderBitmap = renderBitmap(width, (int) (width * k), rectF);
    return renderBitmap.getBitmap();
}
Also used : RectF(android.graphics.RectF) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef)

Example 4 with BitmapRef

use of org.ebookdroid.common.bitmaps.BitmapRef in project LibreraReader by foobnix.

the class DecodeServiceBase method performDecode.

void performDecode(final DecodeTask task) {
    if (executor.isTaskDead(task)) {
        return;
    }
    CodecPageHolder holder = null;
    CodecPage vuPage = null;
    Rect r = null;
    RectF croppedPageBounds = null;
    // TempHolder.lock.lock();
    try {
        holder = getPageHolder(task.id, task.pageNumber);
        vuPage = holder.getPage(task.id);
        if (executor.isTaskDead(task)) {
            return;
        }
        croppedPageBounds = checkCropping(task, vuPage);
        if (executor.isTaskDead(task)) {
            return;
        }
        r = getScaledSize(task.node, task.viewState.zoom, croppedPageBounds, vuPage);
        final RectF actualSliceBounds = task.node.croppedBounds != null ? task.node.croppedBounds : task.node.pageSliceBounds;
        // TempHolder.lock.lock();
        final BitmapRef bitmap = vuPage.renderBitmap(r.width(), r.height(), actualSliceBounds);
        if (executor.isTaskDead(task)) {
            BitmapManager.release(bitmap);
            return;
        }
        // TempHolder.lock.lock();
        if (task.node.page.links == null) {
            task.node.page.links = vuPage.getPageLinks();
            if (LengthUtils.isNotEmpty(task.node.page.links)) {
            }
        }
        if (task.node.page.annotations == null) {
            task.node.page.annotations = vuPage.getAnnotations();
        }
        if (task.node.page.texts == null) {
            task.node.page.texts = vuPage.getText();
        }
        // TempHolder.lock.unlock();
        finishDecoding(task, vuPage, bitmap, r, croppedPageBounds);
    // test
    // vuPage.recycle();
    } catch (final OutOfMemoryError ex) {
        for (int i = 0; i <= AppSettings.getInstance().pagesInMemory; i++) {
            getPages().put(Integer.MAX_VALUE - i, null);
        }
        getPages().clear();
        if (vuPage != null) {
            vuPage.recycle();
        }
        BitmapManager.clear("DecodeService OutOfMemoryError: ");
        abortDecoding(task, null, null);
    } catch (final Throwable th) {
        th.printStackTrace();
        abortDecoding(task, vuPage, null);
    } finally {
        // TempHolder.lock.unlock();
        if (holder != null) {
            holder.unlock();
        }
    }
}
Also used : RectF(android.graphics.RectF) Rect(android.graphics.Rect) CodecPage(org.ebookdroid.core.codec.CodecPage) CodecPageHolder(org.ebookdroid.core.codec.CodecPageHolder) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef)

Example 5 with BitmapRef

use of org.ebookdroid.common.bitmaps.BitmapRef in project LibreraReader by foobnix.

the class ImageExtractor method proccessOtherPage.

public Bitmap proccessOtherPage(PageUrl pageUrl, FileMeta meta) {
    int page = pageUrl.getPage();
    String path = pageUrl.getPath();
    boolean isNeedDisableMagicInPDFDjvu = false;
    LOG.d("Page Number", pageUrl.getPage());
    if (pageUrl.getPage() == COVER_PAGE || pageUrl.getPage() == COVER_PAGE_NO_EFFECT || pageUrl.getPage() == COVER_PAGE_WITH_EFFECT) {
        isNeedDisableMagicInPDFDjvu = true;
    }
    if (page < 0) {
        page = 0;
    }
    if (pageUrl.isCrop()) {
    // isNeedDisableMagicInPDFDjvu = true;
    }
    CodecDocument codeCache = null;
    if (isNeedDisableMagicInPDFDjvu) {
        codeCache = singleCodecContext(path, "", pageUrl.getWidth(), pageUrl.getHeight());
        if (meta != null && codeCache != null && FileMetaCore.isNeedToExtractPDFMeta(path)) {
            String bookAuthor = codeCache.getBookAuthor();
            if (TxtUtils.isNotEmpty(bookAuthor)) {
                meta.setAuthor(bookAuthor);
            }
            String bookTitle = codeCache.getBookTitle();
            if (TxtUtils.isNotEmpty(bookTitle)) {
                meta.setTitle(bookTitle);
            }
            LOG.d("PDF getBookAuthor", bookAuthor, bookTitle);
        }
    } else {
        codeCache = getNewCodecContext(path, "", pageUrl.getWidth(), pageUrl.getHeight());
    }
    if (codeCache == null) {
        LOG.d("TEST", "codecDocument == null" + path);
        return null;
    }
    if (isNeedDisableMagicInPDFDjvu && meta != null) {
        meta.setPages(codeCache.getPageCount());
    }
    final CodecPageInfo pageInfo = codeCache.getPageInfo(page);
    Bitmap bitmap = null;
    RectF rectF = new RectF(0, 0, 1f, 1f);
    final float k = (float) pageInfo.height / pageInfo.width;
    int width = pageUrl.getWidth();
    int height = (int) (width * k);
    LOG.d("Bitmap", width, height);
    LOG.d("Bitmap pageInfo.height", pageInfo.width, pageInfo.height);
    BitmapRef bitmapRef = null;
    CodecPage pageCodec = codeCache.getPage(page);
    if (pageUrl.getNumber() == 0) {
        rectF = new RectF(0, 0, 1f, 1f);
        if (isNeedDisableMagicInPDFDjvu) {
            bitmapRef = pageCodec.renderBitmapSimple(width, height, rectF);
        } else {
            bitmapRef = pageCodec.renderBitmap(width, height, rectF);
        }
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            if (BookType.DJVU.is(pageUrl.getPath())) {
                Bitmap sample = pageCodec.renderBitmapSimple(PageCropper.MAX_WIDTH, PageCropper.MAX_HEIGHT, rectF).getBitmap();
                bitmap = cropBitmap(bitmap, sample);
                sample.recycle();
                sample = null;
            } else {
                bitmap = cropBitmap(bitmap, bitmap);
            }
        }
    } else if (pageUrl.getNumber() == 1) {
        float right = (float) pageUrl.getCutp() / 100;
        rectF = new RectF(0, 0, right, 1f);
        bitmapRef = pageCodec.renderBitmap((int) (width * right), height, rectF);
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            bitmap = cropBitmap(bitmap, bitmap);
        }
    } else if (pageUrl.getNumber() == 2) {
        float right = (float) pageUrl.getCutp() / 100;
        rectF = new RectF(right, 0, 1f, 1f);
        bitmapRef = pageCodec.renderBitmap((int) (width * (1 - right)), height, rectF);
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            bitmap = cropBitmap(bitmap, bitmap);
        }
    }
    if (pageUrl.isInvert()) {
        final RawBitmap bmp = new RawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        bmp.invert();
        bitmap.recycle();
        bitmap = bmp.toBitmap().getBitmap();
    }
    if (pageUrl.getRotate() > 0) {
        final Matrix matrix = new Matrix();
        matrix.postRotate(pageUrl.getRotate());
        final Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        bitmap = bitmap1;
    }
    if (pageUrl.isDoText() && !pageCodec.isRecycled()) {
        PageImageState.get().pagesText.put(pageUrl.getPage(), pageCodec.getText());
        PageImageState.get().pagesLinks.put(pageUrl.getPage(), pageCodec.getPageLinks());
    }
    if (!pageCodec.isRecycled()) {
        pageCodec.recycle();
    }
    if (isNeedDisableMagicInPDFDjvu) {
        codeCache.recycle();
    }
    if (!isNeedDisableMagicInPDFDjvu && MagicHelper.isNeedBookBackgroundImage()) {
        bitmap = MagicHelper.updateWithBackground(bitmap);
    }
    return bitmap;
}
Also used : Rect(android.graphics.Rect) CodecPage(org.ebookdroid.core.codec.CodecPage) RectF(android.graphics.RectF) RawBitmap(org.ebookdroid.common.bitmaps.RawBitmap) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) CodecPageInfo(org.ebookdroid.core.codec.CodecPageInfo) RawBitmap(org.ebookdroid.common.bitmaps.RawBitmap) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef) CodecDocument(org.ebookdroid.core.codec.CodecDocument)

Aggregations

BitmapRef (org.ebookdroid.common.bitmaps.BitmapRef)9 RectF (android.graphics.RectF)5 Rect (android.graphics.Rect)3 CodecPage (org.ebookdroid.core.codec.CodecPage)2 Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 RawBitmap (org.ebookdroid.common.bitmaps.RawBitmap)1 CodecDocument (org.ebookdroid.core.codec.CodecDocument)1 CodecPageHolder (org.ebookdroid.core.codec.CodecPageHolder)1 CodecPageInfo (org.ebookdroid.core.codec.CodecPageInfo)1