<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def encode(data, scheme=None, size=None):
 """ Encodes `data` in a DataMatrix image. For now bpp is the libdmtx default which is 24 Args: data: bytes instance scheme: encoding scheme - one of `ENCODING_SCHEME_NAMES`, or `None`. If `None`, defaults to 'Ascii'. size: image dimensions - one of `ENCODING_SIZE_NAMES`, or `None`. If `None`, defaults to 'ShapeAuto'. Returns: Encoded: with properties `(width, height, bpp, pixels)`. You can use that result to build a PIL image: Image.frombytes('RGB', (width, height), pixels) """


    size = size if size else 'ShapeAuto'
    size_name = '{0}{1}'.format(ENCODING_SIZE_PREFIX, size)
    if not hasattr(DmtxSymbolSize, size_name):
        raise PyLibDMTXError(
            'Invalid size [{0}]: should be one of {1}'.format(
                size, ENCODING_SIZE_NAMES
            )
        )
    size = getattr(DmtxSymbolSize, size_name)

    scheme = scheme if scheme else 'Ascii'
    scheme_name = '{0}{1}'.format(
        ENCODING_SCHEME_PREFIX, scheme.capitalize()
    )
    if not hasattr(DmtxScheme, scheme_name):
        raise PyLibDMTXError(
            'Invalid scheme [{0}]: should be one of {1}'.format(
                scheme, ENCODING_SCHEME_NAMES
            )
        )
    scheme = getattr(DmtxScheme, scheme_name)

    with _encoder() as encoder:
        dmtxEncodeSetProp(encoder, DmtxProperty.DmtxPropScheme, scheme)
        dmtxEncodeSetProp(encoder, DmtxProperty.DmtxPropSizeRequest, size)

        if dmtxEncodeDataMatrix(encoder, len(data), cast(data, c_ubyte_p)) == 0:
            raise PyLibDMTXError(
                'Could not encode data, possibly because the image is not '
                'large enough to contain the data'
            )

        w, h, bpp = map(
            partial(dmtxImageGetProp, encoder[0].image),
            (
                DmtxProperty.DmtxPropWidth, DmtxProperty.DmtxPropHeight,
                DmtxProperty.DmtxPropBitsPerPixel
            )
        )
        size = w * h * bpp // 8
        pixels = cast(
            encoder[0].image[0].pxl, ctypes.POINTER(ctypes.c_ubyte * size)
        )
        return Encoded(
            width=w, height=h, bpp=bpp, pixels=ctypes.string_at(pixels, size)
        )