package rle import ( "fmt" "sync" ) func decompress1(output *[]uint8, width, height int, input []uint8, size int) bool { var ( prevline, line, count int offset, code int x int = width opcode int lastopcode int8 = -1 insertmix, bicolour, isfillormix bool mixmask, mask uint8 colour1, colour2 uint8 mix uint8 = 0xff fom_mask uint8 ) out := *output for len(input) != 0 { fom_mask = 0 code = CVAL(&input) opcode = code >> 4 /* Handle different opcode forms */ switch opcode { case 0xc, 0xd, 0xe: opcode -= 6 count = int(code & 0xf) offset = 16 break case 0xf: opcode = code & 0xf if opcode < 9 { count = int(CVAL(&input)) count |= int(CVAL(&input) << 8) } else { count = 1 if opcode < 0xb { count = 8 } } offset = 0 break default: opcode >>= 1 count = int(code & 0x1f) offset = 32 break } /* Handle strange cases for counts */ if offset != 0 { isfillormix = ((opcode == 2) || (opcode == 7)) if count == 0 { if isfillormix { count = int(CVAL(&input)) + 1 } else { count = int(CVAL(&input) + offset) } } else if isfillormix { count <<= 3 } } /* Read preliminary data */ switch opcode { case 0: /* Fill */ if (lastopcode == int8(opcode)) && !((x == width) && (prevline == 0)) { insertmix = true } break case 8: /* Bicolour */ colour1 = uint8(CVAL(&input)) colour2 = uint8(CVAL(&input)) break case 3: /* Colour */ colour2 = uint8(CVAL(&input)) break case 6: /* SetMix/Mix */ fallthrough case 7: /* SetMix/FillOrMix */ mix = uint8(CVAL(&input)) opcode -= 5 break case 9: /* FillOrMix_1 */ mask = 0x03 opcode = 0x02 fom_mask = 3 break case 0x0a: /* FillOrMix_2 */ mask = 0x05 opcode = 0x02 fom_mask = 5 break } lastopcode = int8(opcode) mixmask = 0 /* Output body */ for count > 0 { if x >= width { if height <= 0 { return false } x = 0 height-- prevline = line line = height * width } switch opcode { case 0: /* Fill */ if insertmix { if prevline == 0 { out[x+line] = mix } else { out[x+line] = out[prevline+x] ^ mix } insertmix = false count-- x++ } n := min(count, width-x) if prevline == 0 { clear(out[x+line : x+line+n]) } else { copy(out[x+line:x+line+n], out[prevline+x:prevline+x+n]) } count -= n x += n break case 1: /* Mix */ n := min(count, width-x) if prevline == 0 { seg := out[x+line : x+line+n] for i := range seg { seg[i] = mix } } else { src := out[prevline+x : prevline+x+n] dst := out[x+line : x+line+n] for i := range dst { dst[i] = src[i] ^ mix } } count -= n x += n break case 2: /* Fill or Mix */ if prevline == 0 { for count > 0 && x < width { mixmask <<= 1 if mixmask == 0 { mask = fom_mask if fom_mask == 0 { mask = uint8(CVAL(&input)) mixmask = 1 } } if mask&mixmask != 0 { out[x+line] = mix } else { out[x+line] = 0 } count-- x++ } } else { for count > 0 && x < width { mixmask = mixmask << 1 if mixmask == 0 { mask = fom_mask if fom_mask == 0 { mask = uint8(CVAL(&input)) mixmask = 1 } } if mask&mixmask != 0 { out[x+line] = out[prevline+x] ^ mix } else { out[x+line] = out[prevline+x] } count-- x++ } } break case 3: /* Colour */ n := min(count, width-x) seg := out[x+line : x+line+n] for i := range seg { seg[i] = colour2 } count -= n x += n break case 4: /* Copy */ for count > 0 && x < width { n := min(count, width-x) if len(input) < n { return false } copy(out[x+line:x+line+n], input[:n]) input = input[n:] count -= n x += n } break case 8: /* Bicolour */ for count > 0 && x < width { if bicolour { out[x+line] = colour2 bicolour = false } else { out[x+line] = colour1 bicolour = true count++ } count-- x++ } break case 0xd: /* White */ n := min(count, width-x) seg := out[x+line : x+line+n] for i := range seg { seg[i] = 0xff } count -= n x += n break case 0xe: /* Black */ n := min(count, width-x) clear(out[x+line : x+line+n]) count -= n x += n break default: fmt.Printf("bitmap opcode 0x%x\n", opcode) return false } } } return true } // decompress2Pool reuses the intermediate []uint16 buffer across calls to // decompress2, avoiding a large per-frame allocation. var decompress2Pool sync.Pool /* 2 byte bitmap decompress */