6
$\begingroup$

I wonder is it possible to render Graphics in Mathematica with bitdepth higher than "Byte" which is the maximum bitdepth supported by the Windows XP operating system (it is so-called "True Color")? Tracing the rasterization with the option TraceInternal->True shows that Graphics is always rendered to a matrix of Integers in the range from 0 to 255, then it may be converted to Reals if requested but such conversion does not increase the bitdepth, of course: it changes only the internal representation of the Image. For example, let us consider rendering of VertexColors:

Cases[Trace[
  Image[Graphics[
    Polygon[{{-1, 0}, {1, 0}, {0, Sqrt[3]}}, 
     VertexColors -> {Red, Green, Blue}], ImageSize -> 4], "Real"], 
  TraceInternal -> True], 
 x_List /; MatrixQ[Unevaluated@x, NumberQ], {1, Infinity}]

One can see matrixes of values between 0 and 255 in spite of requested "Real" bitdepth. Let us compare the results of rendering with different requested bitdepths:

In[21]:= Image[
  Graphics[Polygon[{{-1, 0}, {1, 0}, {0, Sqrt[3]}}, 
    VertexColors -> {Red, Green, Blue}], ImageSize -> 4], "Real"] === 
 Image[Image[
   Graphics[
    Polygon[{{-1, 0}, {1, 0}, {0, Sqrt[3]}}, 
     VertexColors -> {Red, Green, Blue}], ImageSize -> 4], "Byte"], 
  "Real"]

Out[21]= True

It is obvious that the requested bitdepth does not change the real rendering bitdepth.

So the question is: is it possible to render Graphics with bitdepth higher than Byte?

$\endgroup$
8
  • 1
    $\begingroup$ off topic: Windows 7 is said to support 30 and 48 bit color depth. $\endgroup$
    – Silvia
    Commented Jan 11, 2013 at 21:42
  • $\begingroup$ @Silvia Thanks. It would be interesting to know the output of my test code (in the question) under Windows 7 or 8. I am working under Windows XP. $\endgroup$ Commented Jan 12, 2013 at 4:51
  • 2
    $\begingroup$ Related: stackoverflow.com/q/7780327/618728 $\endgroup$
    – Mr.Wizard
    Commented Jan 12, 2013 at 10:19
  • 3
    $\begingroup$ I don't think it's possible to do anything in the kernel. When you rasterize a Graphics expression it is processed by System`ConvertersDump`ConvertGraphicToRasterDataPacket which assembles an ExportPacket and sends it to the front end. The front end returns a System`ConvertersDump`Bitmap expression, which contains compressed bitmap data with a bit depth of 8. The ExportPacket has a ColorSpace option but nothing for bit depth. So I think unless there is a front end option to change the rendering bit depth (I can't see one myself), I suspect the answer is no. $\endgroup$ Commented Jan 12, 2013 at 13:34
  • $\begingroup$ @Simon how did you come by this knowledge? Experimentation? $\endgroup$
    – Mr.Wizard
    Commented Jan 12, 2013 at 13:50

1 Answer 1

5
$\begingroup$

Based on experimentation with Mathematica 8.04 on Windows 7 64-bit:

I don't think it's possible to do anything in the kernel. When you rasterize a Graphics expression it is processed by System`ConvertersDump`ConvertGraphicToRasterDataPacket which assembles an ExportPacket and sends it to the front end. The front end returns a System`ConvertersDump`Bitmap expression, which contains compressed bitmap data with a bit depth of 8. The ExportPacket has a ColorSpace option but nothing for bit depth. So I think unless there is a front end option to change the rendering bit depth (I can't see one myself), I suspect the answer is no.

To investigate on your own system, you can run the Trace looking for calls to the front end:

g = Graphics[
   Polygon[{{-1, 0}, {1, 0}, {0, Sqrt[3]}}, 
    VertexColors -> {Red, Green, Blue}], ImageSize -> 4];

Trace[Image[g, "Real"], MathLink`CallFrontEnd, TraceInternal -> True]

The output on my system contains 4 front end calls, of which the last one is interesting:

enter image description here

You can see that the ExportPacket contains a Notebook expression within which is the box form of the original graphics. Neither the Notebook options or the ExportPacket options contain anything that looks like a bit depth.

The expression returned from the front end is a System`ConvertersDump`Bitmap object, whose first element is a string of strange characters - this is the compressed bitmap data. You can copy the MathLink`CallFrontEnd expression from the trace output and evaluate it to get at the data (copying the compressed data string directly does not seem to work)

result = MathLink`CallFrontEnd[ (** copied from Trace output **)]

The data can be decompressed using:

Developer`RawUncompress[result[[1, 1]]]

{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 234, 184, 182, 188, 188, 165, 178, 233, 177, 255, 255, 255, 231, 216, 222, 74, 74, 107, 212, 229, 218, 255, 255, 255, 255, 255, 255, 183, 183, 232, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}

It is clear that the data is 8 bit.

$\endgroup$
1
  • 2
    $\begingroup$ Very interesting, thank you. In v.8.0.4 this works, but in version 9.0.0 the attempt to Trace MathLink`CallFrontEnd crashes the kernel. $\endgroup$ Commented Jan 13, 2013 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.