Respond to this messageReturn to Index
Original Message
  • I made it even better
    • (no login)
      Posted Jul 20, 2012 6:14 AM

      In my previous program, straight lines would be blurred as well(and straight edges + corners of objects). It would blur the entire boundary of a rectangle and the circle didn't look so good.So I modified it to ignore 1-pixel lines(because from far away it didn't seem to make a difference) and straight edges completely and this is what I ended up with :


      SCREEN _NEWIMAGE(1280, 720, 32)
      LOCATE 5, 20
      PRINT "This is aliased"
      ' Draws some shapes to test the subroutine
      LINE (0, 0)-(720, 720), _RGB(255, 0, 0)
      LINE (110, 110)-(720, 720), _RGB(255, 0, 0), BF
      CIRCLE (500, 500), 100, _RGB(255, 255, 255)
      PAINT (500, 500), _RGB(255, 255, 255), _RGB(255, 255, 255)
      SLEEP
      'call the sub after drawing the shapes.
      s# = TIMER(.001)
      CALL antialias(1280, 720, 1)
      tiaa = TIMER(.001) - s#
      PRINT
      LOCATE 5, 20
      PRINT "This is antialiased. It took "; tiaa; " seconds to antialias the image"
      SLEEP
      CLS
      LINE (0, 0)-(720, 720), _RGB(255, 0, 0)
      LINE (110, 110)-(720, 720), _RGB(255, 0, 0), BF
      CIRCLE (500, 500), 100, _RGB(255, 255, 255)
      PAINT (500, 500), _RGB(255, 255, 255), _RGB(255, 255, 255)
      df# = TIMER(.001)
      CALL blur(1280, 720)
      tiblur = TIMER(.001) - df#
      LOCATE 5, 20
      PRINT "This is blurred. It took "; tiblur; " seconds to blur the image"
      SLEEP
      CLS
      PRINT "Antialiasing was "; (tiblur / tiaa) * 100 - 100; "% faster."

      SUB antialias (screenx, screeny, quality)
      FOR main = 1 TO quality
      ws& = _NEWIMAGE(screenx + 1, screeny + 1, 32)
      _PUTIMAGE (0, 0)-(screenx, screeny), , ws&, (0, 0)-(screenx, screeny)
      _SOURCE ws&
      FOR dx = 1 TO screenx - 1 'goes through each pixel for the entire area of the screen
      FOR dy = 1 TO screeny - 1
      ra = 0
      ga = 0
      ba = 0 'reset color and other values
      difpix = 0
      weightednum = 0
      op~&& = POINT(dx, dy) ' get the color of the main pixel
      FOR cx = -1 TO 1 'gets the 8 pixels around (dx,dy)
      FOR cy = -1 TO 1
      cp~&& = POINT(dx + cx, dy + cy) 'get the color of each of the 8 pixels
      comparison = ABS(_RED32(op~&&) - _RED32(cp~&&)) + ABS(_BLUE32(op~&&) - _BLUE32(cp~&&)) + ABS(_GREEN32(cp~&&) - _GREEN32(op~&&))
      IF comparison > 175 THEN
      difpix = difpix + 1
      END IF
      'check if the colors of the pixels are different enough to antialias them
      weightednum = weightednum + 1
      ra = ra + (_RED32(cp~&&))
      ga = ga + (_GREEN32(cp~&&))
      ba = ba + (_BLUE32(cp~&&))
      'find the average color of (dx,dy) and the 8 pixels surrounding it regardless of whether the pixel needs antialiasing

      NEXT cy
      NEXT cx
      IF difpix > 3 AND difpix < 5 THEN 'if there are different colored pixels among the 8
      '(but not too many otherwise it would antialias a 1-pixel line causing it to lose color. EG. any point on a vertical 1-pixel line has 6 different colored pixels around it)
      ' antialiasing this sort of line would only cause it to lose color

      PSET (dx, dy), _RGB((ra / weightednum), (ga / weightednum), (ba / weightednum))

      'PRINT , dx, dy, _RED32(averagecolor~&&), _GREEN32(averagecolor~&&), _BLUE32(averagecolor~&&)
      'SLEEP
      'For debugging - goes through each pixel which needs to be antialiased and gives its
      ' coordinates and RGB values.
      END IF
      NEXT dy
      NEXT dx
      _FREEIMAGE ws&
      NEXT main
      END SUB

      SUB blur (x, y)
      ws& = _NEWIMAGE(x + 1, y + 1, 32)
      _PUTIMAGE (0, 0)-(x, y), , ws&, (0, 0)-(x, y)
      _SOURCE ws&
      DIM p~&(9)
      FOR cx = 1 TO x - 1 STEP 1
      FOR cy = 1 TO y - 1 STEP 1
      index = 0
      FOR dx = -1 TO 1
      FOR dy = -1 TO 1
      index = index + 1
      p~&(index) = POINT(cx + dx, cy + dy)
      NEXT dy
      NEXT dx
      redaverage~& = 0
      blueaverage~& = 0
      greenaverage~& = 0
      FOR dd = 1 TO 9
      redaverage~& = (_RED32(p~&(dd)) / 9) + redaverage~&
      NEXT dd
      FOR dd = 1 TO 9
      greenaverage~& = (_GREEN32(p~&(dd)) / 9) + greenaverage~&
      NEXT dd
      FOR dd = 1 TO 9
      blueaverage~& = (_BLUE32(p~&(dd)) / 9) + blueaverage~&
      NEXT dd
      PSET (cx, cy), _RGB(redaverage~&, greenaverage~&, blueaverage~&)
      NEXT cy
      NEXT cx
      _FREEIMAGE (ws&)
      END SUB
    Your Name
    Your Email
    (Optional)
    Message Title
    Message Text
    Options Also send responses to my email address