Capturing libraw output as if PGM/PPM data

Some code I am working on uses file i/o redirection and calls the old dcraw under the covers.

Depending on the processing options passed to dcraw, the ouput is passed to the code by the following:

// Intercept the fprintf calls in write_ppm_tiff() for PGM/PPM file output.
int Printf(const char *format, va_list va) // intercepts the fprintf calls in write_ppm_tiff()
int nResult;
CString strText;

strText.FormatV(format, va);
nResult = strText.GetLength();

AddToBuffer(strText.GetBuffer(10000), nResult);

return nResult;
// Intercept the fwrite calls in write_ppm_tiff()
size_t Write (const void *buffer, size_t size, size_t count)
AddToBuffer(buffer, (DWORD)size*(DWORD)count);

return count;

I would like conceptually to do the same sort of thing using libraw but without the need to use the i/o re-direction.

I can (I think) determine whether the data is for PGM or PPM based upon imgdata.idata.colors, and the image rows/columns from imgdata.sizes. I'm not so clear where I can locate the output_bps (to use dcraw terminology).

What I don't know is the best way to get at the image data itself. I assume that I need to call LibRaw::unpack() and then LibRaw::dcraw_process() but I'm bit puzzled as what I do after that.

Thanks in advance