Skip to Content
Author's profile photo Former Member

Calculate Mandelbrot set in ABAP

If you feel bored by crunching through business data, you could create some nice images in ABAP. For example, a fractal. Below is a simple program for calculating a Mandelbrot set. Just add a screen 0100 with a single custom controller, named CUSTOM_CONTAINER. Note that GUI status is not set, so buttons will not be active. You should add them yourself if you need.

/wp-content/uploads/2014/10/mb_563975.jpg

REPORT  ZTEST_FRAC.

TYPES:
   BEGIN OF lty_data,
     data(2556) TYPE x,
   END OF lty_data.

TYPES:
   BEGIN OF lty_cpx,
     x TYPE f,
     y TYPE f,
   END OF lty_cpx.

DATA custom_container TYPE REF TO cl_gui_custom_container.
DATA picture TYPE REF TO cl_gui_picture.
DATA bitmap TYPE xstring.
DATA bitmap_url TYPE char255.
DATA lt_data TYPE TABLE OF lty_data.

DATA:
   BEGIN OF bmp_info,
     bfType(2) TYPE x VALUE ‘424D’, ” BM
     bfSize(4) TYPE x VALUE ‘36100E00’, ” 921654=640*480*3+54 bytes
     bfReserved1(2) TYPE x VALUE ‘0000’,
     bfReserved2(2) TYPE x VALUE ‘0000’,
     bfOffBits(4) TYPE x VALUE ‘36000000’, ” 54=14+40 bytes
     biSize(4) TYPE x VALUE ‘28000000’, ” 40 bytes
     biWidth(4) TYPE x VALUE ‘80020000’, ” 640 pixels
     biHeight(4) TYPE x VALUE ‘E0010000’, ” 480 pixels
     biPlanes(2) TYPE x VALUE ‘0100’, ” 1
     biBitCount(2) TYPE x VALUE ‘2000’, ” 32 bits
     biCompression(4) TYPE x VALUE ‘00000000’,
     biSizeImage(4) TYPE x VALUE ‘00100E00’, ” 921600=640*480*3 bytes
     biXPelsPerMeter(4) TYPE x VALUE ‘00000000’,
     biYPelsPerMeter(4) TYPE x VALUE ‘00000000’,
     biClrUsed(4) TYPE x VALUE ‘000000’,
     biClrImportant(4) TYPE x VALUE ‘000000’,
   END OF bmp_info.

DATA:
   BEGIN OF bmp_color,
     r TYPE x VALUE ’00’,
     g TYPE x VALUE ’00’,
     b TYPE x VALUE ’00’,
     a TYPE x VALUE ’00’,
   END OF bmp_color.

DATA c1 TYPE lty_cpx.
DATA c2 TYPE lty_cpx.
DATA max_l TYPE f.
DATA max_n TYPE i.
DATA c0 TYPE lty_cpx.
DATA z TYPE lty_cpx.
DATA z_temp TYPE lty_cpx.
DATA n TYPE i.
DATA l TYPE f.
DATA dx TYPE i.
DATA dy TYPE i.

DATA lt_points TYPE TABLE OF i.

c1x = ‘-0.9’. c1y = ‘0.2’. ” bottom-left point
c2x = ‘-0.84’. c2y = ‘0.26’. ” top-right point
max_l = 2 * 2. ” square of maximum distance, i.e. “infinity”
max_n = 256. ” maximum number of iterations
dx = 640. ” pixel width (note: bitmap has it hardcoded)
dy = 480. ” pixel height (note: bitmap has it hardcoded)

* Calculate Mandelbrot set
DO dy TIMES.
   c0y = c1y + syindex * ( c2y c1y ) / dy.
   DO dx TIMES.
     c0x = c1x + syindex * ( c2x c1x ) / dx.
     z = c0.
     l = 0.
     n = 0.
     WHILE l < max_l AND n < max_n.
       z_tempx = zx * zx zy * zy + c0x.
       z_tempy = 2 * zx * zy + c0y.
       z = z_temp.
       l = zx * zx + zy * zy.
       ADD 1 TO n.
     ENDWHILE.
     IF n >= max_n.
       n = 0.
     ENDIF.
     APPEND n TO lt_points.
   ENDDO.
ENDDO.

* Fill BMP header
CONCATENATE
     bmp_infobfType
     bmp_infobfSize
     bmp_infobfReserved1
     bmp_infobfReserved2
     bmp_infobfOffBits
     bmp_infobiSize
     bmp_infobiWidth
     bmp_infobiHeight
     bmp_infobiPlanes
     bmp_infobiBitCount
     bmp_infobiCompression
     bmp_infobiSizeImage
     bmp_infobiXPelsPerMeter
     bmp_infobiYPelsPerMeter
     bmp_infobiClrUsed
     bmp_infobiClrImportant
   INTO bitmap IN BYTE MODE.

* Fill BMP pixel array
LOOP AT lt_points INTO n.

   bmp_colorb = n.

   CONCATENATE bitmap
       bmp_colorb
       bmp_colorg
       bmp_colorr
       bmp_colora
     INTO bitmap IN BYTE MODE.

ENDLOOP.

* Prepare and output bitmap in SAP GUI
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
   EXPORTING
     BUFFER                = bitmap
   TABLES
     BINARY_TAB            = lt_data.

CALL FUNCTION ‘DP_CREATE_URL’
      EXPORTING
         type                 = ‘IMAGE’
         subtype              = ‘X-UNKNOWN’
      TABLES
         data                 = lt_data
      CHANGING
         url                  = bitmap_url.

CREATE OBJECT custom_container
   EXPORTING
     container_name = ‘CUSTOM_CONTAINER’.

CREATE OBJECT picture
   EXPORTING parent = custom_container.

picture->load_picture_from_url( exporting url = bitmap_url ).

CALL SCREEN 0100.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Nikolay Evstigneev
      Nikolay Evstigneev

      Awesome!

      Thanks for not giving us become too bored! 🙂

      Author's profile photo Peter Inotai
      Peter Inotai

      Very cool 😀

      Thanks!!!!

      Peter

      Author's profile photo Flavio Furlan
      Flavio Furlan

      WOW! Very cool! I cannot wait until I implement it in my local instance.

      Congrats!