How to convert color EMF spool file to grayscale or Black /white spool file?

Hello

I make R&D virtual printer driver and I am using HP Color LaserJet 8500 PCL driver for
EMF spool file generation. It is not grayscale supported printer driver .SO when I print color
Document to black/ white use with color selection. But the EMF spool files in color.
If I convert EMF to BMP, the BMP size is as per as printer size 5000*4000 …so large. So it takes more time for image processing.
SO
Any one suggests how to convert direct color EMF file to black/ white or gray scale EMF file.
or
Suggest any grayscale, Color and Black/White also default EMF spool file generator Printer name.
Or
Any otherWay?
Thanks
Mahendra
closed account (z05DSL3A)
I have a class (somewhere) than handles EMFs as grey scales, as I remember you have to supply a callback function can on the fly replace the colour values with appropriate grey levels. I'll see if I can find the code (but it will not be until the weekend...as long as I remember).

Another thing you could try is using a generic postscript drive (from Adobe).

-== Edit ==-

To use the code below create a CGrayEMF object and call its EnumEMF() method. I don't think that it converts embedded bitmaps, but that should be able to be added.

-=# The code #=-
NB: The code is from an old project that I inherited, it may need some tweeking to get it to compile. But is does show how to mess with meta files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class CEnumEMF
{
    // virtual function to process every EMF record, return 0 to terminate
    virtual int ProcessRecord(HDC hDC, HANDLETABLE * pHTable, const ENHMETARECORD * pEMFR, int nObj)
    {
        return 0;
    }

    // static callback function, dispatch to virtual function ProcessRecord
    static int CALLBACK EMFProc(HDC hDC, HANDLETABLE * pHTable, 
        const ENHMETARECORD * pEMFR, int nObj, LPARAM lpData)
    {
        CEnumEMF * pObj = (CEnumEMF *) lpData;

        if ( IsBadWritePtr(pObj, sizeof(CEnumEMF)) )
        {
            assert(false);
            return 0;
        }

        return pObj->ProcessRecord(hDC, pHTable, pEMFR, nObj);
    }

public:
    
    BOOL EnumEMF(HDC hDC, HENHMETAFILE hemf, const RECT * lpRect)
    {
        return ::EnumEnhMetaFile(hDC, hemf, EMFProc, this, lpRect);
    }
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
inline void MaptoGray(COLORREF & cr)
{
    if ( (cr & 0xFF000000) != PALETTEINDEX(0) ) // not paletteindex
    {
        BYTE gray = ( GetRValue(cr) * 77 + GetGValue(cr) * 150 + GetBValue(cr) * 29 + 128 ) / 256;
    
        cr = (cr & 0xFF000000) | RGB(gray, gray, gray);
    }
}


class CGrayEMF : public CEnumEMF
{
    // virtual function to process every EMF record, return 0 to terminate
    virtual int ProcessRecord(HDC hDC, HANDLETABLE * pHTable, const ENHMETARECORD * pEMFR, int nObj)
    {
        int rslt;

        switch ( pEMFR->iType )
        {
            case EMR_CREATEBRUSHINDIRECT:
                {
                    EMRCREATEBRUSHINDIRECT cbi;

                    cbi = * (const EMRCREATEBRUSHINDIRECT *) pEMFR;
                    MaptoGray(cbi.lb.lbColor);
                
                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & cbi, nObj);
                }
                break;

            case EMR_CREATEPEN:
                {
                    EMRCREATEPEN cp;

                    cp = * (const EMRCREATEPEN *) pEMFR;
                    MaptoGray(cp.lopn.lopnColor);

                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & cp, nObj);
                }
                break;

            case EMR_SETTEXTCOLOR:
            case EMR_SETBKCOLOR:
                {
                    EMRSETTEXTCOLOR stc;

                    stc = * (const EMRSETTEXTCOLOR *) pEMFR;
                    MaptoGray(stc.crColor);
                
                    rslt = PlayEnhMetaFileRecord(hDC, pHTable, (const ENHMETARECORD *) & stc, nObj);
                }
                break;
            
            case EMR_RESERVED_105:
            case EMR_RESERVED_106:
            case EMR_RESERVED_107:
            case EMR_RESERVED_108:
            case EMR_RESERVED_109:
            case EMR_RESERVED_110:
            case EMR_RESERVED_119:
            case EMR_RESERVED_120:
                rslt = PlayEnhMetaFileRecord(hDC, pHTable, pEMFR, nObj);
                break;

            default:
                rslt = PlayEnhMetaFileRecord(hDC, pHTable, pEMFR, nObj);
        }

        return rslt;
    }
};

Last edited on
Topic archived. No new replies allowed.