public static osd_bitmap osd_new_bitmap(int width, int height, int depth) {

        osd_bitmap bitmap;

        if ((Machine.orientation & ORIENTATION_SWAP_XY) != 0) {
            int temp;

            temp = width;
            width = height;
            height = temp;
        }

        if ((bitmap = new osd_bitmap()) != null) {
            int i, rowlen, rdwidth;
            UBytePtr bm;

            if (depth != 8 && depth != 16) {
                depth = 8;
            }

            bitmap.depth = depth;
            bitmap.width = width;
            bitmap.height = height;

            rdwidth = (width + 7) & ~7;
            /* round width to a quadword */

            if (depth == 16) {
                rowlen = 2 * (rdwidth + 2 * safety);
            } else {
                rowlen = (rdwidth + 2 * safety);
            }

            if ((bm = new UBytePtr((height + 2 * safety) * rowlen)) == null) {
                bitmap = null;
                return null;
            }

            /* clear ALL bitmap, including safety area, to avoid garbage on right */
 /* side of screen is width is not a multiple of 4 */
            memset(bm, 0, (height + 2 * safety) * rowlen);
            if ((bitmap.line = new UBytePtr[((height + 2 * safety))]) == null) {
                bm = null;
                bitmap = null;
                return null;
            }

            for (i = bitmap.ptrPos; i < height + 2 * safety; i++) {
                if (depth == 16) {
                    //bitmap->line[i] = &bm[i * rowlen + 2*safety];
                    bitmap.line[i] = new UBytePtr(bm, (i * rowlen + 2 * safety));

                } else {
                    //bitmap->line[i] = &bm[i * rowlen + safety];
                    bitmap.line[i] = new UBytePtr(bm, (i * rowlen + safety));

                }
                //bitmap.line[i].base += safety;
            }
            //bitmap.line += safety; //moved above TODO check if it's correct (shadow)
            bitmap.ptrPos += safety;
            bitmap._private = bm;

            osd_clearbitmap(bitmap);
        }

        return bitmap;
    }