This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#101 2011-12-02 18:47:29

cocolover76
Scratcher
Registered: 2011-10-09
Posts: 500+

Re: JsScratch viewer development thread

MathWizz wrote:

I have good news and bad news. The bad news is that the player doesn't fix references to other objects properly and will need to be completely recoded. The good news is that I have recoded it.  big_smile  Now to move on to decoding images, decoding sounds, or writing classes (sprites, stage, watchers.)  big_smile   big_smile   big_smile

Read the image code and put it into a variable called image , put the file extension into a variable called imageExt, and do this:

Code:

document.write('<img src="data:image/' + imageExt + ';base64,' + image + '">');

http://i.imgur.com/HfEPZ.gifhttp://i.imgur.com/pvKb6.png

Offline

 

#102 2011-12-03 04:50:18

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: JsScratch viewer development thread

cocolover76 wrote:

MathWizz wrote:

I have good news and bad news. The bad news is that the player doesn't fix references to other objects properly and will need to be completely recoded. The good news is that I have recoded it.  big_smile  Now to move on to decoding images, decoding sounds, or writing classes (sprites, stage, watchers.)  big_smile   big_smile   big_smile

Read the image code and put it into a variable called image , put the file extension into a variable called imageExt, and do this:

Code:

document.write('<img src="data:image/' + imageExt + ';base64,' + image + '">');

Unfortunately, due to the way morphic works, it is slightly more complicated than that to get everything to work with everything else.


http://i.imgur.com/zeIZW.png

Offline

 

#103 2011-12-03 07:38:43

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

rookwood101 wrote:

cocolover76 wrote:

MathWizz wrote:

I have good news and bad news. The bad news is that the player doesn't fix references to other objects properly and will need to be completely recoded. The good news is that I have recoded it.  big_smile  Now to move on to decoding images, decoding sounds, or writing classes (sprites, stage, watchers.)  big_smile   big_smile   big_smile

Read the image code and put it into a variable called image , put the file extension into a variable called imageExt, and do this:

Code:

document.write('<img src="data:image/' + imageExt + ';base64,' + image + '">');

Unfortunately, due to the way morphic works, it is slightly more complicated than that to get everything to work with everything else.

Why does everyone think jQuery, image tags, and events work in canvas?


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#104 2011-12-03 07:42:34

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

I'm checking this out. I have dropping binary images onto canvas in my background.


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#105 2011-12-03 08:26:38

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

Hey math, where's the binary costume data located?

I've noticed in your code you use many statements to fill a class. Why don't you just add them all right in?
You:

Code:

function Person(){

}
Person.prototype.phrase = "hello";
Person.prototype.sayhello = function(){say(this.phrase);};

Why not:

Code:

function Person(){
this.phrase = "hello";
this.sayhello = function(){
this.say(this.phrase);
}
}

http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#106 2011-12-03 10:11:29

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

midnightleopard wrote:

Hey math, where's the binary costume data located?

It's all in a Form in a field named 'bits'.

midnightleopard wrote:

I've noticed in your code you use many statements to fill a class. Why don't you just add them all right in?
You:

Code:

function Person(){

}
Person.prototype.phrase = "hello";
Person.prototype.sayhello = function(){say(this.phrase);};

Why not:

Code:

function Person(){
this.phrase = "hello";
this.sayhello = function(){
this.say(this.phrase);
}
}

I was wondering why I did that myself yesterday.  tongue  If you look at morphic.js, all of the classes are like that so I am just following his example.

Last edited by MathWizz (2011-12-03 10:16:21)


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#107 2011-12-03 10:14:12

cocolover76
Scratcher
Registered: 2011-10-09
Posts: 500+

Re: JsScratch viewer development thread

midnightleopard wrote:

rookwood101 wrote:

cocolover76 wrote:


Read the image code and put it into a variable called image , put the file extension into a variable called imageExt, and do this:

Code:

document.write('<img src="data:image/' + imageExt + ';base64,' + image + '">');

Unfortunately, due to the way morphic works, it is slightly more complicated than that to get everything to work with everything else.

Why does everyone think jQuery, image tags, and events work in canvas?

It was just some code so you'd believe me.
Just make an ImageMorph or something.


http://i.imgur.com/HfEPZ.gifhttp://i.imgur.com/pvKb6.png

Offline

 

#108 2011-12-03 10:18:12

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

Code:

var image = new Image();
image.src = <base64 encoded image here>
image.onload = function()
{
    //insert drawing code here
}

http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#109 2011-12-03 10:20:19

cocolover76
Scratcher
Registered: 2011-10-09
Posts: 500+

Re: JsScratch viewer development thread

MathWizz wrote:

Code:

var image = new Image();
image.src = <base64 encoded image here>
image.onload = function()
{
    //insert drawing code here
}

Code:

var image = new Image();
image.src = "data:image/" + imageExt + ";base64," + image
image.onload = function()
{
    //insert drawing code here
}

http://i.imgur.com/HfEPZ.gifhttp://i.imgur.com/pvKb6.png

Offline

 

#110 2011-12-03 10:26:07

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

I know how to construct a base64 string. -.-


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#111 2011-12-04 19:44:49

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

I was wondering why I did that myself yesterday.  tongue  If you look at morphic.js, all of the classes are like that so I am just following his example.

Well, we've gotten to far to stop, but it makes it alot harder to understand.


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#112 2011-12-04 22:23:55

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

Ok, time to decode images.

Java:

Code:

    void buildImagesAndSounds() throws IOException
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            int j = ((Number)this.objTable[i][1]).intValue();
            Object[] arrayOfObject;
            if ((j == 34) || (j == 35))
            {
                arrayOfObject = (Object[])this.objTable[i][0];
                int k = ((Number)arrayOfObject[0]).intValue();
                int m = ((Number)arrayOfObject[1]).intValue();
                int n = ((Number)arrayOfObject[2]).intValue();
                int[] arrayOfInt = decodePixels(this.objTable[((Ref)arrayOfObject[4]).index][0]);
                MemoryImageSource localMemoryImageSource = null;

                this.objTable[i][0] = empty;
                Object[] localObject2;
                Object localObject1;
                if (n <= 8)
                {
                    if (arrayOfObject[5] != null)
                    {
                        localObject2 = (Object[])this.objTable[((Ref)arrayOfObject[5]).index][0];
                        localObject1 = customColorMap(n, localObject2);
                    }
                    else
                        localObject1 = squeakColorMap(n);
                    localMemoryImageSource = new MemoryImageSource(k, m, (ColorModel)localObject1, rasterToByteRaster(arrayOfInt, k, m, n), 0, k);
                }
                if (n == 16)
                    localMemoryImageSource = new MemoryImageSource(k, m, raster16to32(arrayOfInt, k, m), 0, k);
                if (n == 32)
                    localMemoryImageSource = new MemoryImageSource(k, m, rasterAddAlphaTo32(arrayOfInt), 0, k);
                if (localMemoryImageSource != null)
                {
                    localObject1 = new int[k * m];
                    localObject2 = new PixelGrabber(localMemoryImageSource, 0, 0, k, m, localObject1, 0, k);
                    try
                    {
                        ((PixelGrabber)localObject2).grabPixels();
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                    }
                    BufferedImage localBufferedImage = new BufferedImage(k, m, 2);
                    localBufferedImage.getRaster().setDataElements(0, 0, k, m, localObject1);
                    this.objTable[i][0] = localBufferedImage;
                }
            }

            if (j == 109)
            {
                arrayOfObject = this.objTable[((Ref)this.objTable[i][6]).index];
                this.objTable[i][0] = new ScratchSound(((Number)this.objTable[i][7]).intValue(), (byte[])arrayOfObject[0]);
            }
        }
    }

    int[] decodePixels(Object paramObject) throws IOException
    {
        if ((paramObject instanceof int[]))
            return (int[])paramObject;

        DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream((byte[])paramObject));
        int i = decodeInt(localDataInputStream);
        int[] arrayOfInt = new int[i];
        int j = 0;
        int m;
        int i2;
        int i3;
        while (((localDataInputStream.available() > 0 ? 1 : 0) & (j < i ? 1 : 0)) != 0)
        {
            int k = decodeInt(localDataInputStream);
            m = k >> 2;
            int n = k & 0x3;

            switch (n)
            {
            case 0:
                j += m;
                break;
            case 1:
                int i1 = localDataInputStream.readUnsignedByte();
                i2 = i1 << 24 | i1 << 16 | i1 << 8 | i1;
                for (i3 = 0; i3 < m;)
                {
                    arrayOfInt[(j++)] = i2; i3++;
                    i2 = localDataInputStream.readInt();
                    for (i3 = 0; i3 < m; )
                    {
                        arrayOfInt[(j++)] = i2; i3++;
                        for (i3 = 0; i3 < m; i3++)
                        {
                            i2 = localDataInputStream.readUnsignedByte() << 24;
                            i2 |= localDataInputStream.readUnsignedByte() << 16;
                            i2 |= localDataInputStream.readUnsignedByte() << 8;
                            i2 |= localDataInputStream.readUnsignedByte();
                            arrayOfInt[(j++)] = i2;
                        }
                    }
                }
            case 2:
            case 3:
            }
        }
        return arrayOfInt;
    }

    int decodeInt(DataInputStream paramDataInputStream)    throws IOException
    {
        int i = paramDataInputStream.readUnsignedByte();
        if (i <= 223)
            return i;
        if (i <= 254)
            return (i - 224) * 256 + paramDataInputStream.readUnsignedByte();
        return paramDataInputStream.readInt();
    }

    int[] rasterAddAlphaTo32(int[] paramArrayOfInt)
    {
        for (int i = 0; i < paramArrayOfInt.length; i++)
        {
            int j = paramArrayOfInt[i];
            if (j != 0)
                paramArrayOfInt[i] = (0xFF000000 | j);
        }
        return paramArrayOfInt;
    }

    int[] raster16to32(int[] paramArrayOfInt, int paramInt1, int paramInt2)
    {
        int[] arrayOfInt = new int[paramInt1 * paramInt2];
        int i2 = (paramInt1 + 1) / 2;
        for (int i3 = 0; i3 < paramInt2; i3++)
        {
            int i = 16;
            for (int i4 = 0; i4 < paramInt1; i4++)
            {
                int j = paramArrayOfInt[(i3 * i2 + i4 / 2)] >> i & 0xFFFF;
                int k = (j >> 10 & 0x1F) << 3;
                int m = (j >> 5 & 0x1F) << 3;
                int n = (j & 0x1F) << 3;
                int i1 = k + m + n == 0 ? 0 : 0xFF000000 | k << 16 | m << 8 | n;
                arrayOfInt[(i3 * paramInt1 + i4)] = i1;
                i = i == 16 ? 0 : 16;
            }
        }
        return arrayOfInt;
    }

    byte[] rasterToByteRaster(int[] paramArrayOfInt, int paramInt1, int paramInt2, int paramInt3)
    {
        byte[] arrayOfByte = new byte[paramInt1 * paramInt2];
        int i = paramArrayOfInt.length / paramInt2;
        int j = (1 << paramInt3) - 1;
        int k = 32 / paramInt3;

        for (int m = 0; m < paramInt2; m++)
        {
            for (int n = 0; n < paramInt1; n++)
            {
                int i1 = paramArrayOfInt[(m * i + n / k)];
                int i2 = paramInt3 * (k - n % k - 1);
                arrayOfByte[(m * paramInt1 + n)] = (byte)(i1 >> i2 & j);
            }
        }
        return arrayOfByte;
    }

    IndexColorModel squeakColorMap(int paramInt)
    {
        int i = paramInt == 1 ? -1 : 0;
        return new IndexColorModel(paramInt, 256, squeakColors, 0, false, i);
    }

    IndexColorModel customColorMap(int paramInt, Object[] paramArrayOfObject)
    {
        byte[] arrayOfByte = new byte[4 * paramArrayOfObject.length];
        int i = 0;
        for (int j = 0; j < paramArrayOfObject.length; j++)
        {
            Color localColor = (Color)this.objTable[((Ref)paramArrayOfObject[j]).index][0];
            arrayOfByte[(i++)] = (byte)localColor.getRed();
            arrayOfByte[(i++)] = (byte)localColor.getGreen();
            arrayOfByte[(i++)] = (byte)localColor.getBlue();
            arrayOfByte[(i++)] = (byte)localColor.getAlpha();
        }
        return new IndexColorModel(paramInt, paramArrayOfObject.length, arrayOfByte, 0, true, 0);
    }

    void fixSounds()
    {
        int i = 0;
        int k;
        for (int j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if ((k == 109) && (((ScratchSound)this.objTable[j][0]).isByteReversed()))
                i = 1;
        }

        if (i == 0) return;

        for (j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if (k == 109)
                ((ScratchSound)this.objTable[j][0]).reverseBytes();
        }
    }

    void uncompressMedia()
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            int k = -1;
            if (j >= 100)
                k = ((Number)arrayOfObject[2]).intValue();

            if ((j == 162) && (k >= 4))
            {
                if ((arrayOfObject[7] instanceof byte[]))
                {
                    BufferedImage localBufferedImage = jpegDecode((byte[])arrayOfObject[7]);
                    if (localBufferedImage != null)
                    {
                        if ((arrayOfObject[4] instanceof Image))
                            ((Image)arrayOfObject[4]).flush();
                        arrayOfObject[4] = localBufferedImage;
                        arrayOfObject[7] = empty;
                    }
                }
                if ((arrayOfObject[8] instanceof BufferedImage))
                {
                    arrayOfObject[4] = arrayOfObject[8];
                    arrayOfObject[8] = empty;
                }
            }
            if ((j != 164) || (k < 2) || (!(arrayOfObject[9] instanceof byte[])))
                continue;
            int m = ((Number)arrayOfObject[7]).intValue();
            int n = ((Number)arrayOfObject[8]).intValue();
            byte[] arrayOfByte = (byte[])arrayOfObject[9];
            int i1 = (arrayOfByte.length * 8 + (n - 1)) / n;
            int[] arrayOfInt = new ADPCMDecoder(arrayOfByte, n).decode(i1);
            arrayOfObject[4] = new ScratchSound(m, arrayOfInt);
            Object[] tmp261_260 = (arrayOfObject[9] =  = empty); arrayOfObject[8] = tmp261_260; arrayOfObject[7] = tmp261_260;
        }
    }

    void canonicalizeMedia()
    {
        Vector localVector1 = new Vector(100);
        Vector localVector2 = new Vector(100);

        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            Object localObject;
            if (j == 162)
                localObject = (BufferedImage)arrayOfObject[4];
            if (j == 164)
                localObject = (ScratchSound)arrayOfObject[4];
        }
    }

    BufferedImage jpegDecode(byte[] paramArrayOfByte)
    {
        Toolkit localToolkit = Toolkit.getDefaultToolkit();
        Image localImage = localToolkit.createImage(paramArrayOfByte);

        MediaTracker localMediaTracker = new MediaTracker(canvas);
        localMediaTracker.addImage(localImage, 0);
        try
        {
            localMediaTracker.waitForID(0);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
        
        if (localImage == null)
            return null;

        int i = localImage.getWidth(null);
        int j = localImage.getHeight(null);
        BufferedImage localBufferedImage = new BufferedImage(i, j, 2);
        Graphics localGraphics = localBufferedImage.getGraphics();
        localGraphics.drawImage(localImage, 0, 0, i, j, null);
        localGraphics.dispose();
        localImage.flush();
        return localBufferedImage;
    }

Ok. I'll work on something else now... Jk. *sigh*


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#113 2011-12-05 19:16:02

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

Ok, time to decode images.

Java:

Code:

    void buildImagesAndSounds() throws IOException
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            int j = ((Number)this.objTable[i][1]).intValue();
            Object[] arrayOfObject;
            if ((j == 34) || (j == 35))
            {
                arrayOfObject = (Object[])this.objTable[i][0];
                int k = ((Number)arrayOfObject[0]).intValue();
                int m = ((Number)arrayOfObject[1]).intValue();
                int n = ((Number)arrayOfObject[2]).intValue();
                int[] arrayOfInt = decodePixels(this.objTable[((Ref)arrayOfObject[4]).index][0]);
                MemoryImageSource localMemoryImageSource = null;

                this.objTable[i][0] = empty;
                Object[] localObject2;
                Object localObject1;
                if (n <= 8)
                {
                    if (arrayOfObject[5] != null)
                    {
                        localObject2 = (Object[])this.objTable[((Ref)arrayOfObject[5]).index][0];
                        localObject1 = customColorMap(n, localObject2);
                    }
                    else
                        localObject1 = squeakColorMap(n);
                    localMemoryImageSource = new MemoryImageSource(k, m, (ColorModel)localObject1, rasterToByteRaster(arrayOfInt, k, m, n), 0, k);
                }
                if (n == 16)
                    localMemoryImageSource = new MemoryImageSource(k, m, raster16to32(arrayOfInt, k, m), 0, k);
                if (n == 32)
                    localMemoryImageSource = new MemoryImageSource(k, m, rasterAddAlphaTo32(arrayOfInt), 0, k);
                if (localMemoryImageSource != null)
                {
                    localObject1 = new int[k * m];
                    localObject2 = new PixelGrabber(localMemoryImageSource, 0, 0, k, m, localObject1, 0, k);
                    try
                    {
                        ((PixelGrabber)localObject2).grabPixels();
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                    }
                    BufferedImage localBufferedImage = new BufferedImage(k, m, 2);
                    localBufferedImage.getRaster().setDataElements(0, 0, k, m, localObject1);
                    this.objTable[i][0] = localBufferedImage;
                }
            }

            if (j == 109)
            {
                arrayOfObject = this.objTable[((Ref)this.objTable[i][6]).index];
                this.objTable[i][0] = new ScratchSound(((Number)this.objTable[i][7]).intValue(), (byte[])arrayOfObject[0]);
            }
        }
    }

    int[] decodePixels(Object paramObject) throws IOException
    {
        if ((paramObject instanceof int[]))
            return (int[])paramObject;

        DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream((byte[])paramObject));
        int i = decodeInt(localDataInputStream);
        int[] arrayOfInt = new int[i];
        int j = 0;
        int m;
        int i2;
        int i3;
        while (((localDataInputStream.available() > 0 ? 1 : 0) & (j < i ? 1 : 0)) != 0)
        {
            int k = decodeInt(localDataInputStream);
            m = k >> 2;
            int n = k & 0x3;

            switch (n)
            {
            case 0:
                j += m;
                break;
            case 1:
                int i1 = localDataInputStream.readUnsignedByte();
                i2 = i1 << 24 | i1 << 16 | i1 << 8 | i1;
                for (i3 = 0; i3 < m;)
                {
                    arrayOfInt[(j++)] = i2; i3++;
                    i2 = localDataInputStream.readInt();
                    for (i3 = 0; i3 < m; )
                    {
                        arrayOfInt[(j++)] = i2; i3++;
                        for (i3 = 0; i3 < m; i3++)
                        {
                            i2 = localDataInputStream.readUnsignedByte() << 24;
                            i2 |= localDataInputStream.readUnsignedByte() << 16;
                            i2 |= localDataInputStream.readUnsignedByte() << 8;
                            i2 |= localDataInputStream.readUnsignedByte();
                            arrayOfInt[(j++)] = i2;
                        }
                    }
                }
            case 2:
            case 3:
            }
        }
        return arrayOfInt;
    }

    int decodeInt(DataInputStream paramDataInputStream)    throws IOException
    {
        int i = paramDataInputStream.readUnsignedByte();
        if (i <= 223)
            return i;
        if (i <= 254)
            return (i - 224) * 256 + paramDataInputStream.readUnsignedByte();
        return paramDataInputStream.readInt();
    }

    int[] rasterAddAlphaTo32(int[] paramArrayOfInt)
    {
        for (int i = 0; i < paramArrayOfInt.length; i++)
        {
            int j = paramArrayOfInt[i];
            if (j != 0)
                paramArrayOfInt[i] = (0xFF000000 | j);
        }
        return paramArrayOfInt;
    }

    int[] raster16to32(int[] paramArrayOfInt, int paramInt1, int paramInt2)
    {
        int[] arrayOfInt = new int[paramInt1 * paramInt2];
        int i2 = (paramInt1 + 1) / 2;
        for (int i3 = 0; i3 < paramInt2; i3++)
        {
            int i = 16;
            for (int i4 = 0; i4 < paramInt1; i4++)
            {
                int j = paramArrayOfInt[(i3 * i2 + i4 / 2)] >> i & 0xFFFF;
                int k = (j >> 10 & 0x1F) << 3;
                int m = (j >> 5 & 0x1F) << 3;
                int n = (j & 0x1F) << 3;
                int i1 = k + m + n == 0 ? 0 : 0xFF000000 | k << 16 | m << 8 | n;
                arrayOfInt[(i3 * paramInt1 + i4)] = i1;
                i = i == 16 ? 0 : 16;
            }
        }
        return arrayOfInt;
    }

    byte[] rasterToByteRaster(int[] paramArrayOfInt, int paramInt1, int paramInt2, int paramInt3)
    {
        byte[] arrayOfByte = new byte[paramInt1 * paramInt2];
        int i = paramArrayOfInt.length / paramInt2;
        int j = (1 << paramInt3) - 1;
        int k = 32 / paramInt3;

        for (int m = 0; m < paramInt2; m++)
        {
            for (int n = 0; n < paramInt1; n++)
            {
                int i1 = paramArrayOfInt[(m * i + n / k)];
                int i2 = paramInt3 * (k - n % k - 1);
                arrayOfByte[(m * paramInt1 + n)] = (byte)(i1 >> i2 & j);
            }
        }
        return arrayOfByte;
    }

    IndexColorModel squeakColorMap(int paramInt)
    {
        int i = paramInt == 1 ? -1 : 0;
        return new IndexColorModel(paramInt, 256, squeakColors, 0, false, i);
    }

    IndexColorModel customColorMap(int paramInt, Object[] paramArrayOfObject)
    {
        byte[] arrayOfByte = new byte[4 * paramArrayOfObject.length];
        int i = 0;
        for (int j = 0; j < paramArrayOfObject.length; j++)
        {
            Color localColor = (Color)this.objTable[((Ref)paramArrayOfObject[j]).index][0];
            arrayOfByte[(i++)] = (byte)localColor.getRed();
            arrayOfByte[(i++)] = (byte)localColor.getGreen();
            arrayOfByte[(i++)] = (byte)localColor.getBlue();
            arrayOfByte[(i++)] = (byte)localColor.getAlpha();
        }
        return new IndexColorModel(paramInt, paramArrayOfObject.length, arrayOfByte, 0, true, 0);
    }

    void fixSounds()
    {
        int i = 0;
        int k;
        for (int j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if ((k == 109) && (((ScratchSound)this.objTable[j][0]).isByteReversed()))
                i = 1;
        }

        if (i == 0) return;

        for (j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if (k == 109)
                ((ScratchSound)this.objTable[j][0]).reverseBytes();
        }
    }

    void uncompressMedia()
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            int k = -1;
            if (j >= 100)
                k = ((Number)arrayOfObject[2]).intValue();

            if ((j == 162) && (k >= 4))
            {
                if ((arrayOfObject[7] instanceof byte[]))
                {
                    BufferedImage localBufferedImage = jpegDecode((byte[])arrayOfObject[7]);
                    if (localBufferedImage != null)
                    {
                        if ((arrayOfObject[4] instanceof Image))
                            ((Image)arrayOfObject[4]).flush();
                        arrayOfObject[4] = localBufferedImage;
                        arrayOfObject[7] = empty;
                    }
                }
                if ((arrayOfObject[8] instanceof BufferedImage))
                {
                    arrayOfObject[4] = arrayOfObject[8];
                    arrayOfObject[8] = empty;
                }
            }
            if ((j != 164) || (k < 2) || (!(arrayOfObject[9] instanceof byte[])))
                continue;
            int m = ((Number)arrayOfObject[7]).intValue();
            int n = ((Number)arrayOfObject[8]).intValue();
            byte[] arrayOfByte = (byte[])arrayOfObject[9];
            int i1 = (arrayOfByte.length * 8 + (n - 1)) / n;
            int[] arrayOfInt = new ADPCMDecoder(arrayOfByte, n).decode(i1);
            arrayOfObject[4] = new ScratchSound(m, arrayOfInt);
            Object[] tmp261_260 = (arrayOfObject[9] =  = empty); arrayOfObject[8] = tmp261_260; arrayOfObject[7] = tmp261_260;
        }
    }

    void canonicalizeMedia()
    {
        Vector localVector1 = new Vector(100);
        Vector localVector2 = new Vector(100);

        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            Object localObject;
            if (j == 162)
                localObject = (BufferedImage)arrayOfObject[4];
            if (j == 164)
                localObject = (ScratchSound)arrayOfObject[4];
        }
    }

    BufferedImage jpegDecode(byte[] paramArrayOfByte)
    {
        Toolkit localToolkit = Toolkit.getDefaultToolkit();
        Image localImage = localToolkit.createImage(paramArrayOfByte);

        MediaTracker localMediaTracker = new MediaTracker(canvas);
        localMediaTracker.addImage(localImage, 0);
        try
        {
            localMediaTracker.waitForID(0);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
        
        if (localImage == null)
            return null;

        int i = localImage.getWidth(null);
        int j = localImage.getHeight(null);
        BufferedImage localBufferedImage = new BufferedImage(i, j, 2);
        Graphics localGraphics = localBufferedImage.getGraphics();
        localGraphics.drawImage(localImage, 0, 0, i, j, null);
        localGraphics.dispose();
        localImage.flush();
        return localBufferedImage;
    }

Ok. I'll work on something else now... Jk. *sigh*

jeeze could that be simplified?
I.E: Does it have any x = null; x.prototype.y = z;?


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#114 2011-12-05 19:31:53

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

midnightleopard wrote:

MathWizz wrote:

Ok, time to decode images.

Java:

Code:

    void buildImagesAndSounds() throws IOException
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            int j = ((Number)this.objTable[i][1]).intValue();
            Object[] arrayOfObject;
            if ((j == 34) || (j == 35))
            {
                arrayOfObject = (Object[])this.objTable[i][0];
                int k = ((Number)arrayOfObject[0]).intValue();
                int m = ((Number)arrayOfObject[1]).intValue();
                int n = ((Number)arrayOfObject[2]).intValue();
                int[] arrayOfInt = decodePixels(this.objTable[((Ref)arrayOfObject[4]).index][0]);
                MemoryImageSource localMemoryImageSource = null;

                this.objTable[i][0] = empty;
                Object[] localObject2;
                Object localObject1;
                if (n <= 8)
                {
                    if (arrayOfObject[5] != null)
                    {
                        localObject2 = (Object[])this.objTable[((Ref)arrayOfObject[5]).index][0];
                        localObject1 = customColorMap(n, localObject2);
                    }
                    else
                        localObject1 = squeakColorMap(n);
                    localMemoryImageSource = new MemoryImageSource(k, m, (ColorModel)localObject1, rasterToByteRaster(arrayOfInt, k, m, n), 0, k);
                }
                if (n == 16)
                    localMemoryImageSource = new MemoryImageSource(k, m, raster16to32(arrayOfInt, k, m), 0, k);
                if (n == 32)
                    localMemoryImageSource = new MemoryImageSource(k, m, rasterAddAlphaTo32(arrayOfInt), 0, k);
                if (localMemoryImageSource != null)
                {
                    localObject1 = new int[k * m];
                    localObject2 = new PixelGrabber(localMemoryImageSource, 0, 0, k, m, localObject1, 0, k);
                    try
                    {
                        ((PixelGrabber)localObject2).grabPixels();
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                    }
                    BufferedImage localBufferedImage = new BufferedImage(k, m, 2);
                    localBufferedImage.getRaster().setDataElements(0, 0, k, m, localObject1);
                    this.objTable[i][0] = localBufferedImage;
                }
            }

            if (j == 109)
            {
                arrayOfObject = this.objTable[((Ref)this.objTable[i][6]).index];
                this.objTable[i][0] = new ScratchSound(((Number)this.objTable[i][7]).intValue(), (byte[])arrayOfObject[0]);
            }
        }
    }

    int[] decodePixels(Object paramObject) throws IOException
    {
        if ((paramObject instanceof int[]))
            return (int[])paramObject;

        DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream((byte[])paramObject));
        int i = decodeInt(localDataInputStream);
        int[] arrayOfInt = new int[i];
        int j = 0;
        int m;
        int i2;
        int i3;
        while (((localDataInputStream.available() > 0 ? 1 : 0) & (j < i ? 1 : 0)) != 0)
        {
            int k = decodeInt(localDataInputStream);
            m = k >> 2;
            int n = k & 0x3;

            switch (n)
            {
            case 0:
                j += m;
                break;
            case 1:
                int i1 = localDataInputStream.readUnsignedByte();
                i2 = i1 << 24 | i1 << 16 | i1 << 8 | i1;
                for (i3 = 0; i3 < m;)
                {
                    arrayOfInt[(j++)] = i2; i3++;
                    i2 = localDataInputStream.readInt();
                    for (i3 = 0; i3 < m; )
                    {
                        arrayOfInt[(j++)] = i2; i3++;
                        for (i3 = 0; i3 < m; i3++)
                        {
                            i2 = localDataInputStream.readUnsignedByte() << 24;
                            i2 |= localDataInputStream.readUnsignedByte() << 16;
                            i2 |= localDataInputStream.readUnsignedByte() << 8;
                            i2 |= localDataInputStream.readUnsignedByte();
                            arrayOfInt[(j++)] = i2;
                        }
                    }
                }
            case 2:
            case 3:
            }
        }
        return arrayOfInt;
    }

    int decodeInt(DataInputStream paramDataInputStream)    throws IOException
    {
        int i = paramDataInputStream.readUnsignedByte();
        if (i <= 223)
            return i;
        if (i <= 254)
            return (i - 224) * 256 + paramDataInputStream.readUnsignedByte();
        return paramDataInputStream.readInt();
    }

    int[] rasterAddAlphaTo32(int[] paramArrayOfInt)
    {
        for (int i = 0; i < paramArrayOfInt.length; i++)
        {
            int j = paramArrayOfInt[i];
            if (j != 0)
                paramArrayOfInt[i] = (0xFF000000 | j);
        }
        return paramArrayOfInt;
    }

    int[] raster16to32(int[] paramArrayOfInt, int paramInt1, int paramInt2)
    {
        int[] arrayOfInt = new int[paramInt1 * paramInt2];
        int i2 = (paramInt1 + 1) / 2;
        for (int i3 = 0; i3 < paramInt2; i3++)
        {
            int i = 16;
            for (int i4 = 0; i4 < paramInt1; i4++)
            {
                int j = paramArrayOfInt[(i3 * i2 + i4 / 2)] >> i & 0xFFFF;
                int k = (j >> 10 & 0x1F) << 3;
                int m = (j >> 5 & 0x1F) << 3;
                int n = (j & 0x1F) << 3;
                int i1 = k + m + n == 0 ? 0 : 0xFF000000 | k << 16 | m << 8 | n;
                arrayOfInt[(i3 * paramInt1 + i4)] = i1;
                i = i == 16 ? 0 : 16;
            }
        }
        return arrayOfInt;
    }

    byte[] rasterToByteRaster(int[] paramArrayOfInt, int paramInt1, int paramInt2, int paramInt3)
    {
        byte[] arrayOfByte = new byte[paramInt1 * paramInt2];
        int i = paramArrayOfInt.length / paramInt2;
        int j = (1 << paramInt3) - 1;
        int k = 32 / paramInt3;

        for (int m = 0; m < paramInt2; m++)
        {
            for (int n = 0; n < paramInt1; n++)
            {
                int i1 = paramArrayOfInt[(m * i + n / k)];
                int i2 = paramInt3 * (k - n % k - 1);
                arrayOfByte[(m * paramInt1 + n)] = (byte)(i1 >> i2 & j);
            }
        }
        return arrayOfByte;
    }

    IndexColorModel squeakColorMap(int paramInt)
    {
        int i = paramInt == 1 ? -1 : 0;
        return new IndexColorModel(paramInt, 256, squeakColors, 0, false, i);
    }

    IndexColorModel customColorMap(int paramInt, Object[] paramArrayOfObject)
    {
        byte[] arrayOfByte = new byte[4 * paramArrayOfObject.length];
        int i = 0;
        for (int j = 0; j < paramArrayOfObject.length; j++)
        {
            Color localColor = (Color)this.objTable[((Ref)paramArrayOfObject[j]).index][0];
            arrayOfByte[(i++)] = (byte)localColor.getRed();
            arrayOfByte[(i++)] = (byte)localColor.getGreen();
            arrayOfByte[(i++)] = (byte)localColor.getBlue();
            arrayOfByte[(i++)] = (byte)localColor.getAlpha();
        }
        return new IndexColorModel(paramInt, paramArrayOfObject.length, arrayOfByte, 0, true, 0);
    }

    void fixSounds()
    {
        int i = 0;
        int k;
        for (int j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if ((k == 109) && (((ScratchSound)this.objTable[j][0]).isByteReversed()))
                i = 1;
        }

        if (i == 0) return;

        for (j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if (k == 109)
                ((ScratchSound)this.objTable[j][0]).reverseBytes();
        }
    }

    void uncompressMedia()
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            int k = -1;
            if (j >= 100)
                k = ((Number)arrayOfObject[2]).intValue();

            if ((j == 162) && (k >= 4))
            {
                if ((arrayOfObject[7] instanceof byte[]))
                {
                    BufferedImage localBufferedImage = jpegDecode((byte[])arrayOfObject[7]);
                    if (localBufferedImage != null)
                    {
                        if ((arrayOfObject[4] instanceof Image))
                            ((Image)arrayOfObject[4]).flush();
                        arrayOfObject[4] = localBufferedImage;
                        arrayOfObject[7] = empty;
                    }
                }
                if ((arrayOfObject[8] instanceof BufferedImage))
                {
                    arrayOfObject[4] = arrayOfObject[8];
                    arrayOfObject[8] = empty;
                }
            }
            if ((j != 164) || (k < 2) || (!(arrayOfObject[9] instanceof byte[])))
                continue;
            int m = ((Number)arrayOfObject[7]).intValue();
            int n = ((Number)arrayOfObject[8]).intValue();
            byte[] arrayOfByte = (byte[])arrayOfObject[9];
            int i1 = (arrayOfByte.length * 8 + (n - 1)) / n;
            int[] arrayOfInt = new ADPCMDecoder(arrayOfByte, n).decode(i1);
            arrayOfObject[4] = new ScratchSound(m, arrayOfInt);
            Object[] tmp261_260 = (arrayOfObject[9] =  = empty); arrayOfObject[8] = tmp261_260; arrayOfObject[7] = tmp261_260;
        }
    }

    void canonicalizeMedia()
    {
        Vector localVector1 = new Vector(100);
        Vector localVector2 = new Vector(100);

        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            Object localObject;
            if (j == 162)
                localObject = (BufferedImage)arrayOfObject[4];
            if (j == 164)
                localObject = (ScratchSound)arrayOfObject[4];
        }
    }

    BufferedImage jpegDecode(byte[] paramArrayOfByte)
    {
        Toolkit localToolkit = Toolkit.getDefaultToolkit();
        Image localImage = localToolkit.createImage(paramArrayOfByte);

        MediaTracker localMediaTracker = new MediaTracker(canvas);
        localMediaTracker.addImage(localImage, 0);
        try
        {
            localMediaTracker.waitForID(0);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
        
        if (localImage == null)
            return null;

        int i = localImage.getWidth(null);
        int j = localImage.getHeight(null);
        BufferedImage localBufferedImage = new BufferedImage(i, j, 2);
        Graphics localGraphics = localBufferedImage.getGraphics();
        localGraphics.drawImage(localImage, 0, 0, i, j, null);
        localGraphics.dispose();
        localImage.flush();
        return localBufferedImage;
    }

Ok. I'll work on something else now... Jk. *sigh*

jeeze could that be simplified?
I.E: Does it have any x = null; x.prototype.y = z;?

Umm... Well, they're just methods that need to be translated from Java to JavaScript.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#115 2011-12-05 19:45:14

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

Ok, time to decode images.

Good luck with that one. It took me a while to do, but sadly I modeled my code after the Squeak source, not the Java. I don't think it's that similar, but if it helps you:
- Form stores 32-bit words (split these into bytes to avoid arithmetic rounding errors) compressed into a ByteArray, decompressed in both the static & instance methods of Bitmap. These correspond to pixel values in the Form's depth, look into Color's static methods (I think #pixelValue:depth:)
- ColorForm stores bytes containing color array indices (remember to subtract one from this because Squeak arrays are 1-based) which are also compressed. These point to colors in the colors array, which has 2^depth colors.
For some reason, I had to render ColorForms in rows, and Forms in columns (this might be a bug in my code, though)


nXIII

Offline

 

#116 2011-12-05 19:48:48

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

nXIII wrote:

MathWizz wrote:

Ok, time to decode images.

Good luck with that one. It took me a while to do, but sadly I modeled my code after the Squeak source, not the Java. I don't think it's that similar, but if it helps you:
- Form stores 32-bit words (split these into bytes to avoid arithmetic rounding errors) compressed into a ByteArray, decompressed in both the static & instance methods of Bitmap. These correspond to pixel values in the Form's depth, look into Color's static methods (I think #pixelValue:depth:)
- ColorForm stores bytes containing color array indices (remember to subtract one from this because Squeak arrays are 1-based) which are also compressed. These point to colors in the colors array, which has 2^depth colors.
For some reason, I had to render ColorForms in rows, and Forms in columns (this might be a bug in my code, though)

Are you saying you already accomplished this?!?! Thanks for the hints anyway.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#117 2011-12-05 21:17:25

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

Are you saying you already accomplished this?!?!

Yeah, mostly. There's still a bug with images that are supposed to be larger than they are....


nXIII

Offline

 

#118 2011-12-06 16:53:20

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

midnightleopard wrote:

MathWizz wrote:

Ok, time to decode images.

Java:

Code:

    void buildImagesAndSounds() throws IOException
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            int j = ((Number)this.objTable[i][1]).intValue();
            Object[] arrayOfObject;
            if ((j == 34) || (j == 35))
            {
                arrayOfObject = (Object[])this.objTable[i][0];
                int k = ((Number)arrayOfObject[0]).intValue();
                int m = ((Number)arrayOfObject[1]).intValue();
                int n = ((Number)arrayOfObject[2]).intValue();
                int[] arrayOfInt = decodePixels(this.objTable[((Ref)arrayOfObject[4]).index][0]);
                MemoryImageSource localMemoryImageSource = null;

                this.objTable[i][0] = empty;
                Object[] localObject2;
                Object localObject1;
                if (n <= 8)
                {
                    if (arrayOfObject[5] != null)
                    {
                        localObject2 = (Object[])this.objTable[((Ref)arrayOfObject[5]).index][0];
                        localObject1 = customColorMap(n, localObject2);
                    }
                    else
                        localObject1 = squeakColorMap(n);
                    localMemoryImageSource = new MemoryImageSource(k, m, (ColorModel)localObject1, rasterToByteRaster(arrayOfInt, k, m, n), 0, k);
                }
                if (n == 16)
                    localMemoryImageSource = new MemoryImageSource(k, m, raster16to32(arrayOfInt, k, m), 0, k);
                if (n == 32)
                    localMemoryImageSource = new MemoryImageSource(k, m, rasterAddAlphaTo32(arrayOfInt), 0, k);
                if (localMemoryImageSource != null)
                {
                    localObject1 = new int[k * m];
                    localObject2 = new PixelGrabber(localMemoryImageSource, 0, 0, k, m, localObject1, 0, k);
                    try
                    {
                        ((PixelGrabber)localObject2).grabPixels();
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                    }
                    BufferedImage localBufferedImage = new BufferedImage(k, m, 2);
                    localBufferedImage.getRaster().setDataElements(0, 0, k, m, localObject1);
                    this.objTable[i][0] = localBufferedImage;
                }
            }

            if (j == 109)
            {
                arrayOfObject = this.objTable[((Ref)this.objTable[i][6]).index];
                this.objTable[i][0] = new ScratchSound(((Number)this.objTable[i][7]).intValue(), (byte[])arrayOfObject[0]);
            }
        }
    }

    int[] decodePixels(Object paramObject) throws IOException
    {
        if ((paramObject instanceof int[]))
            return (int[])paramObject;

        DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream((byte[])paramObject));
        int i = decodeInt(localDataInputStream);
        int[] arrayOfInt = new int[i];
        int j = 0;
        int m;
        int i2;
        int i3;
        while (((localDataInputStream.available() > 0 ? 1 : 0) & (j < i ? 1 : 0)) != 0)
        {
            int k = decodeInt(localDataInputStream);
            m = k >> 2;
            int n = k & 0x3;

            switch (n)
            {
            case 0:
                j += m;
                break;
            case 1:
                int i1 = localDataInputStream.readUnsignedByte();
                i2 = i1 << 24 | i1 << 16 | i1 << 8 | i1;
                for (i3 = 0; i3 < m;)
                {
                    arrayOfInt[(j++)] = i2; i3++;
                    i2 = localDataInputStream.readInt();
                    for (i3 = 0; i3 < m; )
                    {
                        arrayOfInt[(j++)] = i2; i3++;
                        for (i3 = 0; i3 < m; i3++)
                        {
                            i2 = localDataInputStream.readUnsignedByte() << 24;
                            i2 |= localDataInputStream.readUnsignedByte() << 16;
                            i2 |= localDataInputStream.readUnsignedByte() << 8;
                            i2 |= localDataInputStream.readUnsignedByte();
                            arrayOfInt[(j++)] = i2;
                        }
                    }
                }
            case 2:
            case 3:
            }
        }
        return arrayOfInt;
    }

    int decodeInt(DataInputStream paramDataInputStream)    throws IOException
    {
        int i = paramDataInputStream.readUnsignedByte();
        if (i <= 223)
            return i;
        if (i <= 254)
            return (i - 224) * 256 + paramDataInputStream.readUnsignedByte();
        return paramDataInputStream.readInt();
    }

    int[] rasterAddAlphaTo32(int[] paramArrayOfInt)
    {
        for (int i = 0; i < paramArrayOfInt.length; i++)
        {
            int j = paramArrayOfInt[i];
            if (j != 0)
                paramArrayOfInt[i] = (0xFF000000 | j);
        }
        return paramArrayOfInt;
    }

    int[] raster16to32(int[] paramArrayOfInt, int paramInt1, int paramInt2)
    {
        int[] arrayOfInt = new int[paramInt1 * paramInt2];
        int i2 = (paramInt1 + 1) / 2;
        for (int i3 = 0; i3 < paramInt2; i3++)
        {
            int i = 16;
            for (int i4 = 0; i4 < paramInt1; i4++)
            {
                int j = paramArrayOfInt[(i3 * i2 + i4 / 2)] >> i & 0xFFFF;
                int k = (j >> 10 & 0x1F) << 3;
                int m = (j >> 5 & 0x1F) << 3;
                int n = (j & 0x1F) << 3;
                int i1 = k + m + n == 0 ? 0 : 0xFF000000 | k << 16 | m << 8 | n;
                arrayOfInt[(i3 * paramInt1 + i4)] = i1;
                i = i == 16 ? 0 : 16;
            }
        }
        return arrayOfInt;
    }

    byte[] rasterToByteRaster(int[] paramArrayOfInt, int paramInt1, int paramInt2, int paramInt3)
    {
        byte[] arrayOfByte = new byte[paramInt1 * paramInt2];
        int i = paramArrayOfInt.length / paramInt2;
        int j = (1 << paramInt3) - 1;
        int k = 32 / paramInt3;

        for (int m = 0; m < paramInt2; m++)
        {
            for (int n = 0; n < paramInt1; n++)
            {
                int i1 = paramArrayOfInt[(m * i + n / k)];
                int i2 = paramInt3 * (k - n % k - 1);
                arrayOfByte[(m * paramInt1 + n)] = (byte)(i1 >> i2 & j);
            }
        }
        return arrayOfByte;
    }

    IndexColorModel squeakColorMap(int paramInt)
    {
        int i = paramInt == 1 ? -1 : 0;
        return new IndexColorModel(paramInt, 256, squeakColors, 0, false, i);
    }

    IndexColorModel customColorMap(int paramInt, Object[] paramArrayOfObject)
    {
        byte[] arrayOfByte = new byte[4 * paramArrayOfObject.length];
        int i = 0;
        for (int j = 0; j < paramArrayOfObject.length; j++)
        {
            Color localColor = (Color)this.objTable[((Ref)paramArrayOfObject[j]).index][0];
            arrayOfByte[(i++)] = (byte)localColor.getRed();
            arrayOfByte[(i++)] = (byte)localColor.getGreen();
            arrayOfByte[(i++)] = (byte)localColor.getBlue();
            arrayOfByte[(i++)] = (byte)localColor.getAlpha();
        }
        return new IndexColorModel(paramInt, paramArrayOfObject.length, arrayOfByte, 0, true, 0);
    }

    void fixSounds()
    {
        int i = 0;
        int k;
        for (int j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if ((k == 109) && (((ScratchSound)this.objTable[j][0]).isByteReversed()))
                i = 1;
        }

        if (i == 0) return;

        for (j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if (k == 109)
                ((ScratchSound)this.objTable[j][0]).reverseBytes();
        }
    }

    void uncompressMedia()
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            int k = -1;
            if (j >= 100)
                k = ((Number)arrayOfObject[2]).intValue();

            if ((j == 162) && (k >= 4))
            {
                if ((arrayOfObject[7] instanceof byte[]))
                {
                    BufferedImage localBufferedImage = jpegDecode((byte[])arrayOfObject[7]);
                    if (localBufferedImage != null)
                    {
                        if ((arrayOfObject[4] instanceof Image))
                            ((Image)arrayOfObject[4]).flush();
                        arrayOfObject[4] = localBufferedImage;
                        arrayOfObject[7] = empty;
                    }
                }
                if ((arrayOfObject[8] instanceof BufferedImage))
                {
                    arrayOfObject[4] = arrayOfObject[8];
                    arrayOfObject[8] = empty;
                }
            }
            if ((j != 164) || (k < 2) || (!(arrayOfObject[9] instanceof byte[])))
                continue;
            int m = ((Number)arrayOfObject[7]).intValue();
            int n = ((Number)arrayOfObject[8]).intValue();
            byte[] arrayOfByte = (byte[])arrayOfObject[9];
            int i1 = (arrayOfByte.length * 8 + (n - 1)) / n;
            int[] arrayOfInt = new ADPCMDecoder(arrayOfByte, n).decode(i1);
            arrayOfObject[4] = new ScratchSound(m, arrayOfInt);
            Object[] tmp261_260 = (arrayOfObject[9] =  = empty); arrayOfObject[8] = tmp261_260; arrayOfObject[7] = tmp261_260;
        }
    }

    void canonicalizeMedia()
    {
        Vector localVector1 = new Vector(100);
        Vector localVector2 = new Vector(100);

        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            Object localObject;
            if (j == 162)
                localObject = (BufferedImage)arrayOfObject[4];
            if (j == 164)
                localObject = (ScratchSound)arrayOfObject[4];
        }
    }

    BufferedImage jpegDecode(byte[] paramArrayOfByte)
    {
        Toolkit localToolkit = Toolkit.getDefaultToolkit();
        Image localImage = localToolkit.createImage(paramArrayOfByte);

        MediaTracker localMediaTracker = new MediaTracker(canvas);
        localMediaTracker.addImage(localImage, 0);
        try
        {
            localMediaTracker.waitForID(0);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
        
        if (localImage == null)
            return null;

        int i = localImage.getWidth(null);
        int j = localImage.getHeight(null);
        BufferedImage localBufferedImage = new BufferedImage(i, j, 2);
        Graphics localGraphics = localBufferedImage.getGraphics();
        localGraphics.drawImage(localImage, 0, 0, i, j, null);
        localGraphics.dispose();
        localImage.flush();
        return localBufferedImage;
    }

Ok. I'll work on something else now... Jk. *sigh*

jeeze could that be simplified?
I.E: Does it have any x = null; x.prototype.y = z;?

Umm... Well, they're just methods that need to be translated from Java to JavaScript.

oh. I thought that was just ridiculously weird javascript. I guess not.


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#119 2011-12-06 17:38:54

cocolover76
Scratcher
Registered: 2011-10-09
Posts: 500+

Re: JsScratch viewer development thread

MathWizz wrote:

Ok, time to decode images.

Java:

Code:

    void buildImagesAndSounds() throws IOException
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            int j = ((Number)this.objTable[i][1]).intValue();
            Object[] arrayOfObject;
            if ((j == 34) || (j == 35))
            {
                arrayOfObject = (Object[])this.objTable[i][0];
                int k = ((Number)arrayOfObject[0]).intValue();
                int m = ((Number)arrayOfObject[1]).intValue();
                int n = ((Number)arrayOfObject[2]).intValue();
                int[] arrayOfInt = decodePixels(this.objTable[((Ref)arrayOfObject[4]).index][0]);
                MemoryImageSource localMemoryImageSource = null;

                this.objTable[i][0] = empty;
                Object[] localObject2;
                Object localObject1;
                if (n <= 8)
                {
                    if (arrayOfObject[5] != null)
                    {
                        localObject2 = (Object[])this.objTable[((Ref)arrayOfObject[5]).index][0];
                        localObject1 = customColorMap(n, localObject2);
                    }
                    else
                        localObject1 = squeakColorMap(n);
                    localMemoryImageSource = new MemoryImageSource(k, m, (ColorModel)localObject1, rasterToByteRaster(arrayOfInt, k, m, n), 0, k);
                }
                if (n == 16)
                    localMemoryImageSource = new MemoryImageSource(k, m, raster16to32(arrayOfInt, k, m), 0, k);
                if (n == 32)
                    localMemoryImageSource = new MemoryImageSource(k, m, rasterAddAlphaTo32(arrayOfInt), 0, k);
                if (localMemoryImageSource != null)
                {
                    localObject1 = new int[k * m];
                    localObject2 = new PixelGrabber(localMemoryImageSource, 0, 0, k, m, localObject1, 0, k);
                    try
                    {
                        ((PixelGrabber)localObject2).grabPixels();
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                    }
                    BufferedImage localBufferedImage = new BufferedImage(k, m, 2);
                    localBufferedImage.getRaster().setDataElements(0, 0, k, m, localObject1);
                    this.objTable[i][0] = localBufferedImage;
                }
            }

            if (j == 109)
            {
                arrayOfObject = this.objTable[((Ref)this.objTable[i][6]).index];
                this.objTable[i][0] = new ScratchSound(((Number)this.objTable[i][7]).intValue(), (byte[])arrayOfObject[0]);
            }
        }
    }

    int[] decodePixels(Object paramObject) throws IOException
    {
        if ((paramObject instanceof int[]))
            return (int[])paramObject;

        DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream((byte[])paramObject));
        int i = decodeInt(localDataInputStream);
        int[] arrayOfInt = new int[i];
        int j = 0;
        int m;
        int i2;
        int i3;
        while (((localDataInputStream.available() > 0 ? 1 : 0) & (j < i ? 1 : 0)) != 0)
        {
            int k = decodeInt(localDataInputStream);
            m = k >> 2;
            int n = k & 0x3;

            switch (n)
            {
            case 0:
                j += m;
                break;
            case 1:
                int i1 = localDataInputStream.readUnsignedByte();
                i2 = i1 << 24 | i1 << 16 | i1 << 8 | i1;
                for (i3 = 0; i3 < m;)
                {
                    arrayOfInt[(j++)] = i2; i3++;
                    i2 = localDataInputStream.readInt();
                    for (i3 = 0; i3 < m; )
                    {
                        arrayOfInt[(j++)] = i2; i3++;
                        for (i3 = 0; i3 < m; i3++)
                        {
                            i2 = localDataInputStream.readUnsignedByte() << 24;
                            i2 |= localDataInputStream.readUnsignedByte() << 16;
                            i2 |= localDataInputStream.readUnsignedByte() << 8;
                            i2 |= localDataInputStream.readUnsignedByte();
                            arrayOfInt[(j++)] = i2;
                        }
                    }
                }
            case 2:
            case 3:
            }
        }
        return arrayOfInt;
    }

    int decodeInt(DataInputStream paramDataInputStream)    throws IOException
    {
        int i = paramDataInputStream.readUnsignedByte();
        if (i <= 223)
            return i;
        if (i <= 254)
            return (i - 224) * 256 + paramDataInputStream.readUnsignedByte();
        return paramDataInputStream.readInt();
    }

    int[] rasterAddAlphaTo32(int[] paramArrayOfInt)
    {
        for (int i = 0; i < paramArrayOfInt.length; i++)
        {
            int j = paramArrayOfInt[i];
            if (j != 0)
                paramArrayOfInt[i] = (0xFF000000 | j);
        }
        return paramArrayOfInt;
    }

    int[] raster16to32(int[] paramArrayOfInt, int paramInt1, int paramInt2)
    {
        int[] arrayOfInt = new int[paramInt1 * paramInt2];
        int i2 = (paramInt1 + 1) / 2;
        for (int i3 = 0; i3 < paramInt2; i3++)
        {
            int i = 16;
            for (int i4 = 0; i4 < paramInt1; i4++)
            {
                int j = paramArrayOfInt[(i3 * i2 + i4 / 2)] >> i & 0xFFFF;
                int k = (j >> 10 & 0x1F) << 3;
                int m = (j >> 5 & 0x1F) << 3;
                int n = (j & 0x1F) << 3;
                int i1 = k + m + n == 0 ? 0 : 0xFF000000 | k << 16 | m << 8 | n;
                arrayOfInt[(i3 * paramInt1 + i4)] = i1;
                i = i == 16 ? 0 : 16;
            }
        }
        return arrayOfInt;
    }

    byte[] rasterToByteRaster(int[] paramArrayOfInt, int paramInt1, int paramInt2, int paramInt3)
    {
        byte[] arrayOfByte = new byte[paramInt1 * paramInt2];
        int i = paramArrayOfInt.length / paramInt2;
        int j = (1 << paramInt3) - 1;
        int k = 32 / paramInt3;

        for (int m = 0; m < paramInt2; m++)
        {
            for (int n = 0; n < paramInt1; n++)
            {
                int i1 = paramArrayOfInt[(m * i + n / k)];
                int i2 = paramInt3 * (k - n % k - 1);
                arrayOfByte[(m * paramInt1 + n)] = (byte)(i1 >> i2 & j);
            }
        }
        return arrayOfByte;
    }

    IndexColorModel squeakColorMap(int paramInt)
    {
        int i = paramInt == 1 ? -1 : 0;
        return new IndexColorModel(paramInt, 256, squeakColors, 0, false, i);
    }

    IndexColorModel customColorMap(int paramInt, Object[] paramArrayOfObject)
    {
        byte[] arrayOfByte = new byte[4 * paramArrayOfObject.length];
        int i = 0;
        for (int j = 0; j < paramArrayOfObject.length; j++)
        {
            Color localColor = (Color)this.objTable[((Ref)paramArrayOfObject[j]).index][0];
            arrayOfByte[(i++)] = (byte)localColor.getRed();
            arrayOfByte[(i++)] = (byte)localColor.getGreen();
            arrayOfByte[(i++)] = (byte)localColor.getBlue();
            arrayOfByte[(i++)] = (byte)localColor.getAlpha();
        }
        return new IndexColorModel(paramInt, paramArrayOfObject.length, arrayOfByte, 0, true, 0);
    }

    void fixSounds()
    {
        int i = 0;
        int k;
        for (int j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if ((k == 109) && (((ScratchSound)this.objTable[j][0]).isByteReversed()))
                i = 1;
        }

        if (i == 0) return;

        for (j = 0; j < this.objTable.length; j++)
        {
            k = ((Number)this.objTable[j][1]).intValue();
            if (k == 109)
                ((ScratchSound)this.objTable[j][0]).reverseBytes();
        }
    }

    void uncompressMedia()
    {
        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            int k = -1;
            if (j >= 100)
                k = ((Number)arrayOfObject[2]).intValue();

            if ((j == 162) && (k >= 4))
            {
                if ((arrayOfObject[7] instanceof byte[]))
                {
                    BufferedImage localBufferedImage = jpegDecode((byte[])arrayOfObject[7]);
                    if (localBufferedImage != null)
                    {
                        if ((arrayOfObject[4] instanceof Image))
                            ((Image)arrayOfObject[4]).flush();
                        arrayOfObject[4] = localBufferedImage;
                        arrayOfObject[7] = empty;
                    }
                }
                if ((arrayOfObject[8] instanceof BufferedImage))
                {
                    arrayOfObject[4] = arrayOfObject[8];
                    arrayOfObject[8] = empty;
                }
            }
            if ((j != 164) || (k < 2) || (!(arrayOfObject[9] instanceof byte[])))
                continue;
            int m = ((Number)arrayOfObject[7]).intValue();
            int n = ((Number)arrayOfObject[8]).intValue();
            byte[] arrayOfByte = (byte[])arrayOfObject[9];
            int i1 = (arrayOfByte.length * 8 + (n - 1)) / n;
            int[] arrayOfInt = new ADPCMDecoder(arrayOfByte, n).decode(i1);
            arrayOfObject[4] = new ScratchSound(m, arrayOfInt);
            Object[] tmp261_260 = (arrayOfObject[9] =  = empty); arrayOfObject[8] = tmp261_260; arrayOfObject[7] = tmp261_260;
        }
    }

    void canonicalizeMedia()
    {
        Vector localVector1 = new Vector(100);
        Vector localVector2 = new Vector(100);

        for (int i = 0; i < this.objTable.length; i++)
        {
            Object[] arrayOfObject = this.objTable[i];
            int j = ((Number)arrayOfObject[1]).intValue();
            Object localObject;
            if (j == 162)
                localObject = (BufferedImage)arrayOfObject[4];
            if (j == 164)
                localObject = (ScratchSound)arrayOfObject[4];
        }
    }

    BufferedImage jpegDecode(byte[] paramArrayOfByte)
    {
        Toolkit localToolkit = Toolkit.getDefaultToolkit();
        Image localImage = localToolkit.createImage(paramArrayOfByte);

        MediaTracker localMediaTracker = new MediaTracker(canvas);
        localMediaTracker.addImage(localImage, 0);
        try
        {
            localMediaTracker.waitForID(0);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
        
        if (localImage == null)
            return null;

        int i = localImage.getWidth(null);
        int j = localImage.getHeight(null);
        BufferedImage localBufferedImage = new BufferedImage(i, j, 2);
        Graphics localGraphics = localBufferedImage.getGraphics();
        localGraphics.drawImage(localImage, 0, 0, i, j, null);
        localGraphics.dispose();
        localImage.flush();
        return localBufferedImage;
    }

Ok. I'll work on something else now... Jk. *sigh*

I am ultimately sorry, but we are working with Javascript.


http://i.imgur.com/HfEPZ.gifhttp://i.imgur.com/pvKb6.png

Offline

 

#120 2011-12-06 18:07:34

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

cocolover76 wrote:

MathWizz wrote:

*stuff*

I am ultimately sorry, but we are working with Javascript.

*ahem* I am the one that has created the whole player to this date (with the exception of morphic.js) and I am being told that I am programming in JavaScript?
...

Last edited by MathWizz (2011-12-06 18:08:45)


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#121 2011-12-06 20:15:52

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

cocolover76 wrote:

MathWizz wrote:

*stuff*

I am ultimately sorry, but we are working with Javascript.

*ahem* I am the one that has created the whole player to this date (with the exception of morphic.js) and I am being told that I am programming in JavaScript?
...

who added cocolover to the team?
(Just asking, I don't adding you.)


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#122 2011-12-06 20:17:10

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

midnightleopard wrote:

who added cocolover to the team?
(Just asking, I don't adding you.)

I didn't.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#123 2011-12-06 20:46:15

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: JsScratch viewer development thread

Hey MathWhiz? Do you know if it would be possible to write it (well, the entire project in general, but the image-thing in particular) in JavaScript?  How would you access the image data (assuming it's possible)?


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#124 2011-12-06 21:00:20

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: JsScratch viewer development thread

MoreGamesNow wrote:

Hey MathWhiz? Do you know if it would be possible to write it (well, the entire project in general, but the image-thing in particular) in JavaScript?  How would you access the image data (assuming it's possible)?

Are you asking if it is possible to read images (such as costumes) from a Scratch file in JavaScript? That's what I am trying to figure out right now.  tongue


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#125 2011-12-07 08:12:54

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: JsScratch viewer development thread

MathWizz wrote:

Are you asking if it is possible to read images (such as costumes) from a Scratch file in JavaScript? That's what I am trying to figure out right now.  tongue

Darn it!  And yes, that was what i was asking  big_smile   How are you getting the Scratch code with Java (my experience with Java is pretty much zero, so if it isn't possible to give me a simple answer, just say so  smile  )?


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

Board footer