home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / EXAMPLES / 256COLOR.TXT < prev    next >
Text File  |  1997-09-04  |  5KB  |  113 lines

  1.                              =====================
  2.                              = 256 color example =
  3.                              =====================
  4.  
  5.  
  6. Overview
  7. --------
  8.  
  9. This example shows how to work with 8bit indexed images and palettes.
  10. It starts by initializing PTC, Creating an 8bit surface, setting up the
  11. surface and display palette then drawing a color gradient across the surface.
  12. This surface is then updated to the display.
  13.  
  14.  
  15.  
  16. Creating the surface
  17. --------------------
  18.  
  19. >    // create fullscreen 256 color surface
  20. >    Surface surface(ptc,xres,yres,INDEX8);
  21. >    if (!surface.ok())
  22. >    {
  23. >        ptc.Close();
  24. >        cout << "could not create 256 color surface\n";
  25. >        return 1;
  26. >    }
  27.  
  28. The surface is created with the INDEX8 pixel format to the resolutions of the
  29. display. Note that the format INDEX8 is also used as the default format when
  30. command line PTC initialize fails. Why not just "ptc.Init(320,200,8)" ?
  31. Because we specifically want an 8bit indexed mode with a palette. If we simply
  32. passed 8bits to the PTC::Init or PTC::SetMode the set mode will set ANY 8bit
  33. mode, perhaps a mode that is a direct color mode (like RGB332) that packs into
  34. 8bits. Clearly, it is best to describe the format fully using a format id
  35. or format object (see "format.h" and "globals.h") than just setting a mode by
  36. bits per pixel alone.
  37.  
  38.  
  39.  
  40. Creating the palette
  41. --------------------
  42.  
  43. >    // create palette
  44. >    Palette palette;
  45. >    uint* data=(uint*)palette.Lock();
  46. >    if (!data) return 1;
  47. >    for (int i=0; i<256; i++) data[i]=RGB32(i,0,i);
  48. >    palette.Unlock();
  49.  
  50. The code above creates the palette, locks it (just as you would lock a
  51. surface), writes to the palette date, then unlocks. Internally the data of a
  52. palette object is currently fixed at 256 entries of ARGB8888 format color 
  53. data. This is why the RGB32 function is used to pack the r,g,b entries into 
  54. the palette data (the same function used to pack pixels in rand32).
  55.  
  56. As with surfaces you should always check the data returned when you lock a
  57. palette. A palette lock will return NULL if the palette object is invalid.
  58. You can check a Palette object is valid with the Palette::ok function.
  59.  
  60.  
  61.  
  62. Setting the surface palette
  63. ---------------------------
  64.  
  65. >    // set surface palette
  66. >    surface.SetPalette(palette);
  67.  
  68. The above code sets the palette of the surface to the purple palette we just
  69. created. Its important to note that when an indexed format surface is created,
  70. the palette data is always initialized to zero. If you don't set your own
  71. palette, chances are things will look a bit too dark for your liking.
  72.  
  73. Also, regarding 8bit surfaces and palettes - when you BitBlt between two 8bit
  74. surfaces the palettes are NOT taken into account. The index data is just
  75. copied verbatim from one surface to the other. No translation or color
  76. matching is performed.
  77.  
  78. If you are a bit smart, you would be thinking "well, if the palette isn't
  79. really taken into account when you BitBlt, then why do I need to set the
  80. surface palette here at all? Why cant I just set the display palette?". Well
  81. the answer is that you could, and you wouldn't run into any problems if all
  82. you did was BitBlt between 8bit surfaces. However, you run into problems when
  83. you BitBlt from 8bit -> other pixel formats. The converters from 8bit to other
  84. formats like direct color (hicolor/truecolor) formats take the source palette
  85. into account so if you haven't setup your surfaces palette then you see
  86. nothing but black.
  87.  
  88.  
  89. It is for this reason that we set both the surface palette and the display
  90. palette (via the PTC object) like so:
  91.  
  92. >    // set display palette if required
  93. >    FORMAT format=ptc.GetFormat();
  94. >    if (format.type==INDEXED && format.model!=GREYSCALE) ptc.SetPalette(palette);
  95.  
  96. Why the weird if (...) check? All this does is make sure we only set the
  97. palette if the PTC object is in an indexed mode that is NOT greyscale. Why
  98. don't we set the palette if we are in a greyscale format? The reason is that
  99. while you are in a greyscale output mode such as GREY8, PTC allows you to
  100. customize the greyscale color ramp - so you could have a greyscale of black
  101. to blue or whatever you want. The problem occurs when you set a "bad" palette
  102. when you are in a greyscale mode. Obviously if you set a palette that is
  103. rather random and doesn't form a smooth gradient the GREY8 image will look
  104. NOTHING like greyscale, so it is for this reason that we make sure we leave
  105. the greyscale palette alone in this program.
  106.  
  107. Again, you wont be needing to check if you are in a greyscale mode before
  108. every palette set that you do - unless you are writing a program that needs
  109. to be as flexible as this one is, which is highly unlikely. There are only a
  110. few situations where you would want to BitBlt from 8bit to truecolor and this
  111. is not one of them :)
  112.  
  113.