Package bdsup2sub.bitmap

Examples of bdsup2sub.bitmap.Palette


     */
    private Palette decodePalette(final SubPictureHD pic) throws CoreException {
        int ofs = pic.getPaletteOffset();
        int alphaOfs = pic.getAlphaOffset();

        Palette palette = new Palette(256);
        try {
            for (int i=0; i < palette.getSize(); i++) {
                // each palette entry consists of 3 bytes
                int y = buffer.getByte(ofs++);
                int cr,cb;
                if (configuration.isSwapCrCb()) {
                    cb = buffer.getByte(ofs++);
                    cr = buffer.getByte(ofs++);
                } else {
                    cr = buffer.getByte(ofs++);
                    cb = buffer.getByte(ofs++);
                }
                // each alpha entry consists of 1 byte
                int alpha = 0xff - buffer.getByte(alphaOfs++);
                if (alpha < configuration.getAlphaCrop()) { // to not mess with scaling algorithms, make transparent color black
                    palette.setRGB(i, 0, 0, 0);
                } else {
                    palette.setYCbCr(i, y, cb, cr);
                }
                palette.setAlpha(i, alpha);
            }
            return palette;
        } catch (FileBufferException ex) {
            throw new CoreException (ex.getMessage());
        }
View Full Code Here


            int rgbSrc[] = subtitleStream.getPalette().getRGB(subtitleStream.getPrimaryColorIndex());

            // match with primary color from 16 color target palette
            // note: skip index 0 , primary colors at even positions
            // special treatment for index 1:  white
            Palette trgPallete = currentDVDPalette;
            int minDistance = 0xffffff; // init > 0xff*0xff*3 = 0x02fa03
            int colIdx = 0;
            for (int idx=1; idx<trgPallete.getSize(); idx+=2 )  {
                int rgb[] = trgPallete.getRGB(idx);
                // distance vector (skip sqrt)
                int rd = rgbSrc[0]-rgb[0];
                int gd = rgbSrc[1]-rgb[1];
                int bd = rgbSrc[2]-rgb[2];
                int distance = rd*rd+gd*gd+bd*bd;
                // new minimum distance ?
                if ( distance < minDistance) {
                    colIdx = idx;
                    minDistance = distance;
                    if (minDistance == 0) {
                        break;
                    }
                }
                // special treatment for index 1 (white)
                if (idx == 1) {
                    idx--; // -> continue with index = 2
                }
            }

            // set new frame palette
            int palFrame[] = new int[4];
            palFrame[0] = 0;        // black - transparent color
            palFrame[1] = colIdx;   // primary color
            if (colIdx == 1) {
                palFrame[2] = colIdx+2; // special handling: white + dark grey
            } else {
                palFrame[2] = colIdx+1; // darker version of primary color
            }
            palFrame[3] = 0;        // black - opaque

            subVobTrg.setAlpha(DEFAULT_ALPHA);
            subVobTrg.setPal(palFrame);

            trgPal = SupDvdUtil.decodePalette(subVobTrg, trgPallete);
        } else {
            // use palette from loaded VobSub or SUP/IFO
            Palette miniPal = new Palette(4, true);
            int alpha[];
            int palFrame[];
            DvdSubtitleStream substreamDvd;

            if (inMode == InputMode.VOBSUB) {
                substreamDvd = subDVD;
            } else {
                substreamDvd = supDVD;
            }

            alpha = substreamDvd.getFrameAlpha(index);
            palFrame = substreamDvd.getFramePalette(index);

            for (int i=0; i < 4; i++) {
                int a = (alpha[i]*0xff)/0xf;
                if (a >= configuration.getAlphaCrop()) {
                    miniPal.setARGB(i, currentSourceDVDPalette.getARGB(palFrame[i]));
                    miniPal.setAlpha(i, a);
                } else {
                    miniPal.setARGB(i, 0);
                }
            }
            subVobTrg.setAlpha(alpha);
            subVobTrg.setPal(palFrame);
            trgPal = miniPal;
View Full Code Here

        // decode first frame
        subtitleStream.decode(0);
        subVobTrg = new SubPictureDVD();
        defaultSourceDVDPalette = substreamDvd.getSrcPalette();
        currentSourceDVDPalette = new Palette(defaultSourceDVDPalette);

        // automatically set luminance thresholds for VobSub conversion
        int primColIdx = subtitleStream.getPrimaryColorIndex();
        int yMax = subtitleStream.getPalette().getY()[primColIdx] & 0xff;
        int[] luminanceThreshold = new int[2];
View Full Code Here

                default:
                    f = null;
            }

            Bitmap tBm;
            Palette tPal = trgPal;
            // create scaled bitmap
            OutputMode outputMode = configuration.getOutputMode();
            PaletteMode paletteMode = configuration.getPaletteMode();
            if (outputMode == OutputMode.VOBSUB || outputMode == OutputMode.SUPIFO) {
                // export 4 color palette
                if (w==trgWidth && h==trgHeight) {
                    // don't scale at all
                    if ( (inMode == InputMode.VOBSUB || inMode == InputMode.SUPIFO) && paletteMode == PaletteMode.KEEP_EXISTING) {
                        tBm = subtitleStream.getBitmap(); // no conversion
                    } else {
                        tBm = subtitleStream.getBitmap().getBitmapWithNormalizedPalette(subtitleStream.getPalette().getAlpha(), configuration.getAlphaThreshold(), subtitleStream.getPalette().getY(), configuration.getLuminanceThreshold()); // reduce palette
                    }
                } else {
                    // scale up/down
                    if ((inMode == InputMode.VOBSUB || inMode == InputMode.SUPIFO) && paletteMode == PaletteMode.KEEP_EXISTING) {
                        // keep palette
                        if (f != null) {
                            tBm = subtitleStream.getBitmap().scaleFilter(trgWidth, trgHeight, subtitleStream.getPalette(), f);
                        } else {
                            tBm = subtitleStream.getBitmap().scaleBilinear(trgWidth, trgHeight, subtitleStream.getPalette());
                        }
                    } else {
                        // reduce palette
                        if (f != null) {
                            tBm = subtitleStream.getBitmap().scaleFilterLm(trgWidth, trgHeight, subtitleStream.getPalette(), configuration.getAlphaThreshold(), configuration.getLuminanceThreshold(), f);
                        } else {
                            tBm = subtitleStream.getBitmap().scaleBilinearLm(trgWidth, trgHeight, subtitleStream.getPalette(), configuration.getAlphaThreshold(), configuration.getLuminanceThreshold());
                        }
                    }
                }
            } else {
                // export (up to) 256 color palette
                tPal = subtitleStream.getPalette();
                if (w==trgWidth && h==trgHeight) {
                    tBm = subtitleStream.getBitmap(); // no scaling, no conversion
                } else {
                    // scale up/down
                    if (paletteMode == PaletteMode.KEEP_EXISTING) {
                        // keep palette
                        if (f != null) {
                            tBm = subtitleStream.getBitmap().scaleFilter(trgWidth, trgHeight, subtitleStream.getPalette(), f);
                        } else {
                            tBm = subtitleStream.getBitmap().scaleBilinear(trgWidth, trgHeight, subtitleStream.getPalette());
                        }
                    } else {
                        // create new palette
                        boolean dither = paletteMode == PaletteMode.CREATE_DITHERED;
                        BitmapWithPalette pb;
                        if (f != null) {
                            pb = subtitleStream.getBitmap().scaleFilter(trgWidth, trgHeight, subtitleStream.getPalette(), f, dither);
                        } else {
                            pb = subtitleStream.getBitmap().scaleBilinear(trgWidth, trgHeight, subtitleStream.getPalette(), dither);
                        }
                        tBm = pb.bitmap;
                        tPal = pb.palette;
                    }
                }
            }
            if (!picTrg.getErasePatch().isEmpty()) {
                trgBitmapUnpatched = new Bitmap(tBm);
                int col = tPal.getIndexOfMostTransparentPaletteEntry();
                for (ErasePatch ep : picTrg.getErasePatch()) {
                    tBm.fillRectangularWithColorIndex(ep.x, ep.y, ep.width, ep.height, (byte)col);
                }
            } else {
                trgBitmapUnpatched = tBm;
View Full Code Here

            }
        }

        boolean importedDVDPalette = (inMode == InputMode.VOBSUB) || (inMode == InputMode.SUPIFO);

        Palette trgPallete = null;
        PaletteMode paletteMode = configuration.getPaletteMode();
        if (outputMode == OutputMode.VOBSUB) {
            // VobSub - write IDX
            /* return offsets as array of ints */
            int[] ofs = new int[offsets.size()];
View Full Code Here

        List<PaletteInfo> paletteInfos = pic.getPalettes().get(pic.getImageObject().getPaletteID());
        if (paletteInfos == null) {
            throw new CoreException("Palette ID out of bounds.");
        }

        Palette palette = new Palette(256, Core.usesBT601());
        // by definition, index 0xff is always completely transparent
        // also all entries must be fully transparent after initialization

        try {
            for (PaletteInfo paletteInfo : paletteInfos) {
                int index = paletteInfo.getPaletteOffset();
                for (int i = 0; i < paletteInfo.getPaletteSize(); i++) {
                    // each palette entry consists of 5 bytes
                    paletteIndex = buffer.getByte(index);
                    int y = buffer.getByte(++index);
                    int cr, cb;
                    if (configuration.isSwapCrCb()) {
                        cb = buffer.getByte(++index);
                        cr = buffer.getByte(++index);
                    } else {
                        cr = buffer.getByte(++index);
                        cb = buffer.getByte(++index);
                    }
                    int alpha = buffer.getByte(++index);

                    int alphaOld = palette.getAlpha(paletteIndex);
                    // avoid fading out
                    if (alpha >= alphaOld) {
                        if (alpha < configuration.getAlphaCrop()) {// to not mess with scaling algorithms, make transparent color black
                            y = 16;
                            cr = 128;
                            cb = 128;
                        }
                        palette.setAlpha(paletteIndex, alpha);
                    } else {
                        fadeOut = true;
                    }

                    palette.setYCbCr(paletteIndex, y, cb, cr);
                    index++;
                }
            }
            if (fadeOut) {
                logger.warn("fade out detected -> patched palette\n");
View Full Code Here

                logger.warn("Quantizer failed.\n");
            } else {
                logger.trace("Palette had to be reduced from " + pal.getSize() + " to " + size + " entries.\n");
            }
            // create palette
            pal = new Palette(size);
            for (int i=0; i < size; i++) {
                pal.setARGB(i,ct[i]);
            }
            // use new bitmap
            bm = bmQ;
View Full Code Here

            // first try to read image and palette directly from imported image
            if (img.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
                IndexColorModel icm = (IndexColorModel)img.getColorModel();
                if (icm.getMapSize() < 255 || (icm.hasAlpha() && icm.getAlpha(255) == 0)) {
                    // create palette
                    palette = new Palette(256);
                    for (int i=0; i < icm.getMapSize(); i++) {
                        int alpha = (icm.getRGB(i) >> 24) & 0xff;
                        if (alpha >= configuration.getAlphaCrop()) {
                            palette.setARGB(i, icm.getRGB(i));
                        } else {
                            palette.setARGB(i, 0);
                        }
                    }
                    // copy pixels
                    WritableRaster raster = img.getRaster();
                    bitmap = new Bitmap(img.getWidth(), img.getHeight(), (byte[])raster.getDataElements( 0, 0, img.getWidth(), img.getHeight(), null ));
                }
            }

            // if this failed, assume RGB image and quantize palette
            if (palette == null) {
                // grab int array (ARGB)
                int[] pixels = new int[w * h];
                img.getRGB(0, 0, w, h, pixels, 0, w);
                // quantize image
                QuantizeFilter qf = new QuantizeFilter();
                bitmap = new Bitmap(img.getWidth(), img.getHeight());
                int ct[] = qf.quantize(pixels, bitmap.getInternalBuffer(), w, h, 255, false, false);
                int size = ct.length;
                if (size > 255) {
                    logger.warn("Quantizer failed.\n");
                    size = 255;
                }
                // create palette
                palette = new Palette(256);
                for (int i=0; i < size; i++) {
                    int alpha = (ct[i] >> 24) & 0xff;
                    if (alpha >= configuration.getAlphaCrop()) {
                        palette.setARGB(i, ct[i]);
                    } else {
View Full Code Here

            DvdPaletteDialog dvdPaletteDialog = new DvdPaletteDialog(view, colorNames, currentColors, defaultColors, model.getColorProfilePath());
            dvdPaletteDialog.setVisible(true);
            if (!dvdPaletteDialog.wasCanceled()) {
                currentColors = dvdPaletteDialog.getColors();
                model.setColorProfilePath(dvdPaletteDialog.getPath());
                Palette p = new Palette(currentColors.length, true);
                for (int i=0; i<currentColors.length; i++) {
                    p.setColor(i, currentColors[i]);
                }
                Core.setCurSrcDVDPalette(p);

                try {
                    if (Core.isReady()) {
View Full Code Here

    void addResetButtonActionListener(ActionListener actionListener) {
        jButtonReset.addActionListener(actionListener);
    }

    private void initComboBoxColorPreviewIcons(FramePaletteDialogModel model) {
        Palette palette = Core.getCurSrcDVDPalette();
        model.setColorPreviewIcon(new ImageIcon[16]);
        for (int i=0; i < palette.getSize(); i++) {
            model.getColorPreviewIcon()[i] = new ImageIcon(new BufferedImage(12, 12, BufferedImage.TYPE_INT_RGB));
            Color c = new Color (palette.getARGB(i) | 0xff000000); // make fully opaque
            paintIcon(model.getColorPreviewIcon()[i], c);
            jComboBoxColor1.addItem(COLOR_NAME[i]);
            jComboBoxColor2.addItem(COLOR_NAME[i]);
            jComboBoxColor3.addItem(COLOR_NAME[i]);
            jComboBoxColor4.addItem(COLOR_NAME[i]);
View Full Code Here

TOP

Related Classes of bdsup2sub.bitmap.Palette

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.