Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    SetPixel() for 32 bit float images

    Cinema 4D SDK
    python r20 sdk
    2
    3
    845
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      owen
      last edited by owen

      In the class c4d.bitmaps.BaseBitmap you can call BaseBitmap.SetPixel() to set the color value of an individual pixel. It seems like it only takes in values between 0 and 255 for RGB. I'm working with MultipassBitmap to create a 32 bit float image and want to be able to modify the individual pixels. Is there a similar way to change the pixel values so I could feed it a value between 0 and 1?

      1 Reply Last reply Reply Quote 0
      • S
        s_bach
        last edited by

        Hello,

        you can write 32bit data into a BaseBitmap using SetPixelCnt(). This function allows to write a block of data into the bitmap.

        You find example code showing how to use SetPixelCnt() in the documentation, on GitHub or here:

        bitmap = c4d.bitmaps.BaseBitmap()
        if bitmap is None:
            return
        
        # define bitmap dimensions and bit depth
        width = 100
        height = 100
        pixelBytes = c4d.COLORBYTES_RGBf # RGBf format
        pixelBits = pixelBytes * 8
        
        # initialize the BaseBitmap with the given dimensions and bit depth
        res = bitmap.Init(width, height, pixelBits)
        if res != c4d.IMAGERESULT_OK:
            return
        
        # allocate memory for one line
        bufferSize = pixelBytes * width
        lineBuffer = storage.ByteSeq(None, bufferSize)
        
        # loop through all lines
        for y in xrange(height):
            
            # shade of red based on the position
            red = float(y) / float(height)
            
            offset = 0
            for x in xrange(width):
                # shade of green based on the position
                green = float(x) / float(width)
                
                # fill buffer
                lineBuffer[offset + 0:offset + 4] = struct.pack("f",red)
                lineBuffer[offset + 4:offset + 8] = struct.pack("f", green)
                lineBuffer[offset + 8:offset + 12] = struct.pack("f", 0.0)
                
                offset = offset + pixelBytes
                
            # write full line into the bitmap    
            bitmap.SetPixelCnt(0, y, width, lineBuffer.GetOffset(0), pixelBytes, c4d.COLORMODE_RGBf, c4d.PIXELCNT_0)
                
                
        c4d.bitmaps.ShowBitmap(bitmap)
        

        As always, please add tags to your post.

        best wishes,
        Sebastian

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 1
        • O
          owen
          last edited by

          Thanks so much this is exactly what I was looking for.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post