MR image: Difference between revisions

From dreamcast.wiki
Jump to navigation Jump to search
Tag: Reverted
m (Reverted edits by Darc (talk) to last revision by Unknown user)
Tag: Rollback
 
Line 72: Line 72:
# input: Byte array
# input: Byte array
# output: Byte array
# output: Byte array
# size: Size of input in bytes (image width * image height)
# input_size: Size of input in bytes (image width * image height)
# returns: Size of the compressed data
# returns: Size of the compressed data
def mr_encode(input, output, size):
def mr_encode(input, output, input_size):
    run = 0
     length = 0
     length = 0
     position = 0
     position = 0
    run = 0
 
   
     while(position < input_size):
     while(position < size):
         run = 1
         run = 1


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


Line 112: Line 112:
     return length
     return length
</syntaxhighlight>
</syntaxhighlight>
==Decoding Algorithm==
==Decoding Algorithm==
<syntaxhighlight lang="c" line="line">
<syntaxhighlight lang="c" line="line">
# input: Byte array
# input: Byte array
# cdata_size: Size of input in bytes
# input_size: Size of input in bytes
# idata_size: Size of uncompressed data in bytes (image width * image height)
# uncompressed_size: Size of uncompressed data in bytes (image width * image height)
# returns: Decoded image data
# returns: Decoded image data
def mr_decode(input, cdata_size, idata_size):
def mr_decode(input, input_size, uncompressed_size):
    run = 0
     position = 0
     position = 0
     idx_position = 0
     idx_position = 0
    run = 0
     indexed_data = array("B", "\x00" * uncompressed_size)
     indexed_data = array("B", "\x00" * idata_size)


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


Line 152: Line 151:
         # Writing decompressed bytes
         # Writing decompressed bytes
         for i in range(run):
         for i in range(run):
            # The additional byte (+ 1) is useless, but it always present in MR files.
             if(idx_position+i < uncompressed_size):
             if(idx_position+i < idata_size):
                 indexed_data[idx_position+i] = first_byte
                 indexed_data[idx_position+i] = first_byte



Latest revision as of 00:23, 11 October 2023

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.

File Header

Size (bytes) Contents
2 "MR"
4 Total file size
4 Crap (fill with 0's)
4 Image data offset in bytes (Header size + Palette size)
4 Image width
4 Image height
4 Crap (fill with 0's)
4 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