MR image: Difference between revisions

From dreamcast.wiki
Jump to navigation Jump to search
(Added algorithms and finishing touches to about section)
No edit summary
Line 60: Line 60:


==Encoding Algorithm==
==Encoding Algorithm==
<pre>
<syntaxhighlight lang="c" line="line">
# input: Byte array
# input: Byte array
# output: Byte array
# output: Byte array
Line 102: Line 102:


     return length
     return length
</pre>
</syntaxhighlight>
==Decoding Algorithm==
==Decoding Algorithm==
<pre>
<syntaxhighlight lang="c" line="line">
# input: Byte array
# input: Byte array
# input_size: Size of input in bytes
# input_size: Size of input in bytes
Line 148: Line 148:


     return indexed_data
     return indexed_data
</pre>
</syntaxhighlight>

Revision as of 12:52, 30 May 2020

About

A MR image is an image format solely used inside an IP.BIN file to show off a logo during the boot screen process of the Sega Dreamcast.

Games powered by Windows CE boot screen

This image format was used in commercialized Dreamcast games utilizing Windows CE SDK where we see a "Powered by Microsoft Windows CE" logo. It is now commonly used in homebrew to distinguish the creator(s) of the application or to disassociate the application being produced by or under license from Sega. The MR image must be inserted into a IP.BIN file at offset 0x3820 in order to be viewed during boot sequence. Since IP.BIN is restricted to a 32k file size, there are a couple of guidelines the MR image must meet to in order to fit inside the IP.BIN.


MR Image must be:

  • 320x90 or less
  • Max 128 colors
  • Less than 8192 bytes to fit in a IP.BIN
  • The transparent color is #c0c0c0, or 192, 192, 192 in RGB


Tools

Selfboot-Inducer

SiZious's Windows Tool to create homebrew compilations. Using this application you can view and create MR images from other various other image formats.


MR GIMP Plugin

GIMP is a cross-platform image editor available for GNU/Linux, OS X, Windows.

Using this GIMP plugin("file-mr.py"), you can create view, edit, and create a MR image from any other image format that GIMP supports.

File Format

The file format is composed of three sections: Header, Palette, and the Image Data. All of the data written in the file is in little-endian format.

A MR file starts off with a 30 byte header.

Header

Size Contents
2 bytes "MR"
4 bytes Total file size
4 bytes Crap (fill with 0's)
4 bytes Image data offset in bytes (Header size + Palette size)
4 bytes Image width
4 bytes Image height
4 bytes Crap (fill with 0's)
4 bytes Amount of colors in palette

Palette

The header is followed by the image's palette. The palette is composed of palette entries where each palette entry is 4 bytes long and is stored as BGRA. The alpha byte goes unused. The maximum number of palette entries is 128.

Palette Entry
B G R A

In total, the byte size of your palette should be number of colors * 4.

Image Data

Lastly, the image data. The image data is basically an array of indices, each a byte long, that refers to a palette entry in the palette. The image data is compressed using a form of Run-Length Encoding (RLE). The python algorithms to encode/decode the image data are shown below.

Encoding Algorithm

# input: Byte array
# output: Byte array
# input_size: Size of input in bytes (image width * image height)
# returns: Size of the compressed data
def mr_encode(input, output, input_size):
    run = 0
    length = 0
    position = 0
   
    while(position < input_size):
        run = 1

        while((run < 0x17f) and (position+run < input_size) and (input[position] == input[position+run])):
            run += 1

        if(run > 0xff):
            output[length] = 0x82
            length += 1
            output[length] = 0x80 | (run - 0x100)
            length += 1
            output[length] = input[position]
            length += 1
        elif(run > 0x7f):
            output[length] = 0x81
            length += 1
            output[length] = run
            length += 1
            output[length] = input[position]
            length += 1
        elif(run > 1):
            output[length] = 0x80 | run
            length += 1
            output[length] = input[position]
            length += 1
        else:
            output[length] = input[position]
            length += 1
        
        position += run

    return length

Decoding Algorithm

# input: Byte array
# input_size: Size of input in bytes
# uncompressed_size: Size of uncompressed data in bytes (image width * image height)
# returns: Decoded image data
def mr_decode(input, input_size, uncompressed_size):
    run = 0
    position = 0
    idx_position = 0
    indexed_data = array("B", "\x00" * uncompressed_size)

    while(position < input_size):
        first_byte = input[position]
        if((position+1) < input_size):
            second_byte = input[position+1]

        # The bytes lower than 0x80 are recopied just as they are in the Bitmap
        if(first_byte < 0x80):
            run = 1
            position += 1
        # The tag 0x81 is followed by a byte giving directly the count of points
        elif(first_byte == 0x81):
            run = second_byte
            first_byte = input[position+2]
            position += 3
        # The tag 0x82 is followed by the number of the points decoded in Run
        # By retaining only the first byte for each point
        elif(first_byte == 0x82 and second_byte >= 0x80):
            run = second_byte - 0x80 + 0x100
            first_byte = input[position+2]
            position += 3
        else:
            run = first_byte - 0x80
            first_byte = second_byte
            position += 2

        # Writing decompressed bytes
        for i in range(run):
            if(idx_position+i < uncompressed_size):
                indexed_data[idx_position+i] = first_byte

        idx_position += run

    return indexed_data