Results 1 to 3 of 3

Thread: Run-Length Decoding of almost-random data

  1. #1
    Member Alexander Rhatushnyak's Avatar
    Join Date
    Oct 2007
    Location
    Canada
    Posts
    250
    Thanks
    46
    Thanked 105 Times in 54 Posts

    Smile Run-Length Decoding of almost-random data

    The attached small Python script creates an image that looks,
    if you turn on your imagination and look from different angles, |_ \_ --.--
    like a flower, or maybe a head with a couple eyes and a smiling mouth...
    Zoom level should be set to 100% during the looking with imagination exercise

    Code:
    import math,sympy
    Size = 1025  # must be an odd number
    offset = Size>>1  # so the center is at (512,512) if Size==1025 
    
    def get_x_y(n):  # this function is from http://oeis.org/A235913
      sr = int(math.sqrt(n-1))  # OK for relatively small n's
      sr = sr-1+(sr&1)
      rm = n-sr*sr
      d = (sr+1)/2
      if rm<=sr+1:
         return -d+rm, d
      if rm<=sr*2+2:
         return d, d-(rm-(sr+1))
      if rm<=sr*3+3:
         return d-(rm-(sr*2+2)), -d
      return -d, -d+rm-(sr*3+3)
    
    outfile = open("outfile_42.ppm", "wb")
    outfile.write("P6\n"+str(Size)+" "+str(Size)+"\n255\n")
    data = [4] * (Size*Size)
    p = runLength = 1
    print 'Starting the main loop...'
    for i in range(Size*Size):            # runs ~2 seconds if Size==1025
            runLength -= 1
            if runLength==0:
                p = sympy.nextprime(p+1)
                colorCode = (p & 7) >> 1   #  extract 3 bits, discard the last bit, it's always a 1
                runLength = (p >> 3) % 42 + 1  #  42 is a magic number
                x,y = get_x_y(i+1)
                data[x+offset+(y+offset)*Size] = colorCode
    
    print 'Writing image file...'
    for x in range(Size):        # also ~2 seconds if Size==1025
      for y in range(Size):
        rgb = [0,0,0]
        v = data[x+y*Size]
        if v<4:
          numNeighbors = 0
          for i in range(x-3, x+3 +1):
            for j in range(y-3, y+3 +1):
              if i>=0 and j>=0 and i<Size and j<Size and data[i+j*Size]<4:  numNeighbors+=1
          if numNeighbors > 1:   #  apply some noise reduction.  > 0 to skip noise reduction
            if v<3: rgb[v] = 255
            elif v==3:  rgb[0] = rgb[1] = rgb[2] = 255
        for c in rgb: outfile.write(chr(c))
    outfile.close()
    Last edited by Alexander Rhatushnyak; 22nd December 2017 at 06:56.

    This newsgroup is dedicated to image compression:
    http://linkedin.com/groups/Image-Compression-3363256

  2. Thanks:

    encode (9th January 2018)

  3. #2
    Member Alexander Rhatushnyak's Avatar
    Join Date
    Oct 2007
    Location
    Canada
    Posts
    250
    Thanks
    46
    Thanked 105 Times in 54 Posts
    Same code but Size=1199 and magic number 110 instead of 42: http://qlic.altervista.org/size1199_mn110.gif
    Horizontal lines near the bottom, something with many visible edges near the upper-right corner.

    This newsgroup is dedicated to image compression:
    http://linkedin.com/groups/Image-Compression-3363256

  4. #3
    Member Alexander Rhatushnyak's Avatar
    Join Date
    Oct 2007
    Location
    Canada
    Posts
    250
    Thanks
    46
    Thanked 105 Times in 54 Posts
    If anyone is interested in reading & commenting
    the text & slides of my talk at Starcon a month ago,
    please let me know, I'll email them to you.

    This newsgroup is dedicated to image compression:
    http://linkedin.com/groups/Image-Compression-3363256

  5. Thanks:

    Mike (8th February 2018)

Similar Threads

  1. TurboRLE: Turbo Run Length Encoding
    By dnd in forum Data Compression
    Replies: 25
    Last Post: 20th August 2019, 09:21
  2. Implementing Run-length encoding in CUDA
    By nemequ in forum Data Compression
    Replies: 1
    Last Post: 28th June 2016, 14:08
  3. Replies: 38
    Last Post: 27th April 2016, 19:01
  4. xrle-eXtreme Run Length Encoding
    By algorithm in forum Data Compression
    Replies: 5
    Last Post: 21st April 2015, 00:02
  5. Random Data Question.
    By Tribune in forum Data Compression
    Replies: 7
    Last Post: 13th June 2008, 20:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •