Skip to content

Instantly share code, notes, and snippets.

@henriqueblang
Last active June 13, 2020 03:04
Show Gist options
  • Save henriqueblang/e07348d1b1a7ec6adfcbdc1a82abbe87 to your computer and use it in GitHub Desktop.
Save henriqueblang/e07348d1b1a7ec6adfcbdc1a82abbe87 to your computer and use it in GitHub Desktop.

Revisions

  1. henriqueblang revised this gist Jun 13, 2020. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions rle.py
    Original file line number Diff line number Diff line change
    @@ -52,10 +52,7 @@ def RLE_zigzag_encoder(data, rows, columns):

    return encoding

    def RLE_zigzag_decoder(data, rows, columns):
    UP = 0
    DOWN = 1

    def RLE_zigzag_decoder(data, rows, columns):
    decoding = [[None] * columns for i in range(rows)]

    iterator = 0
  2. henriqueblang revised this gist Jun 13, 2020. 1 changed file with 11 additions and 14 deletions.
    25 changes: 11 additions & 14 deletions rle.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,10 @@
    def RLE_zigzag_encoder(data, rows, columns):
    UP = 0
    DOWN = 1

    def RLE_zigzag_encoder(data, rows, columns):
    encoding = []

    prev = data[0][0]
    count = 1

    direction = None
    go_down = None
    change_direction = True

    next_position = [0, 1]
    @@ -32,12 +29,12 @@ def RLE_zigzag_encoder(data, rows, columns):
    next_position[0] += 1
    next_position[1] -= 1

    direction = DOWN
    go_down = True
    elif next_position[1] == 0 or next_position[0] == last_position[0]:
    next_position[0] -= 1
    next_position[1] += 1

    direction = UP
    go_down = False
    elif next_position[0] == 0 or next_position[0] == last_position[0]:
    next_position[1] += 1

    @@ -47,8 +44,8 @@ def RLE_zigzag_encoder(data, rows, columns):

    change_direction = True
    else:
    next_position[0] += 1 * (direction == DOWN and 1 or -1)
    next_position[1] += 1 * (direction == DOWN and -1 or 1)
    next_position[0] += 1 * (go_down and 1 or -1)
    next_position[1] += 1 * (go_down and -1 or 1)

    encoding.append(count)
    encoding.append(prev)
    @@ -62,7 +59,7 @@ def RLE_zigzag_decoder(data, rows, columns):
    decoding = [[None] * columns for i in range(rows)]

    iterator = 0
    direction = None
    go_down = None
    change_direction = False

    repeat = data[iterator]
    @@ -96,12 +93,12 @@ def RLE_zigzag_decoder(data, rows, columns):
    next_position[0] += 1
    next_position[1] -= 1

    direction = DOWN
    go_down = True
    elif next_position[1] == 0 or next_position[0] == last_position[0]:
    next_position[0] -= 1
    next_position[1] += 1

    direction = UP
    go_down = False
    elif next_position[0] == 0 or next_position[0] == last_position[0]:
    next_position[1] += 1

    @@ -111,8 +108,8 @@ def RLE_zigzag_decoder(data, rows, columns):

    change_direction = True
    else:
    next_position[0] += 1 * (direction == DOWN and 1 or -1)
    next_position[1] += 1 * (direction == DOWN and -1 or 1)
    next_position[0] += 1 * (go_down and 1 or -1)
    next_position[1] += 1 * (go_down and -1 or 1)

    return decoding

  3. henriqueblang created this gist Jun 13, 2020.
    134 changes: 134 additions & 0 deletions rle.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    def RLE_zigzag_encoder(data, rows, columns):
    UP = 0
    DOWN = 1

    encoding = []

    prev = data[0][0]
    count = 1

    direction = None
    change_direction = True

    next_position = [0, 1]
    last_position = (rows - 1, columns - 1)

    while next_position[0] <= last_position[0] and next_position[1] <= last_position[1]:
    value = data[next_position[0]][next_position[1]]

    if value != prev:
    encoding.append(count)
    encoding.append(prev)

    count = 1
    prev = value
    else:
    count += 1

    if change_direction:
    change_direction = False

    if next_position[1] == last_position[1] or next_position[0] == 0:
    next_position[0] += 1
    next_position[1] -= 1

    direction = DOWN
    elif next_position[1] == 0 or next_position[0] == last_position[0]:
    next_position[0] -= 1
    next_position[1] += 1

    direction = UP
    elif next_position[0] == 0 or next_position[0] == last_position[0]:
    next_position[1] += 1

    change_direction = True
    elif next_position[1] == 0 or next_position[1] == last_position[1]:
    next_position[0] += 1

    change_direction = True
    else:
    next_position[0] += 1 * (direction == DOWN and 1 or -1)
    next_position[1] += 1 * (direction == DOWN and -1 or 1)

    encoding.append(count)
    encoding.append(prev)

    return encoding

    def RLE_zigzag_decoder(data, rows, columns):
    UP = 0
    DOWN = 1

    decoding = [[None] * columns for i in range(rows)]

    iterator = 0
    direction = None
    change_direction = False

    repeat = data[iterator]
    iterator += 1

    next_position = [0, 0]
    last_position = (rows - 1, columns - 1)

    data_len = len(data)
    while True:
    value = data[iterator]

    if repeat == 0:
    iterator += 1

    if iterator == data_len:
    break

    repeat = data[iterator]
    iterator += 1

    continue

    decoding[next_position[0]][next_position[1]] = value
    repeat -= 1

    if change_direction:
    change_direction = False

    if next_position[1] == last_position[1] or next_position[0] == 0:
    next_position[0] += 1
    next_position[1] -= 1

    direction = DOWN
    elif next_position[1] == 0 or next_position[0] == last_position[0]:
    next_position[0] -= 1
    next_position[1] += 1

    direction = UP
    elif next_position[0] == 0 or next_position[0] == last_position[0]:
    next_position[1] += 1

    change_direction = True
    elif next_position[1] == 0 or next_position[1] == last_position[1]:
    next_position[0] += 1

    change_direction = True
    else:
    next_position[0] += 1 * (direction == DOWN and 1 or -1)
    next_position[1] += 1 * (direction == DOWN and -1 or 1)

    return decoding

    array = [
    [1, 1, 0, 4, 0, 0],
    [1, 1, 4, 0, 0, 0],
    [1, 8, 0, 0, 0, 0],
    [4, 0, 0, 2, 0, 0],
    [4, 1, 0, 0, 0, 0]
    ]

    enc = RLE_zigzag_encoder(array, 5, 6)

    print(enc)

    dec = RLE_zigzag_decoder(enc, 5, 6)

    print(dec)
    print(array)