Retrieving Color Inormation of Bitmaps 
Author Message
 Retrieving Color Inormation of Bitmaps

How can I retrieve the color information of bitmaps by using REXX
commands or functions. I'd like to read any bitmap pixel by pixel and
check the color information (RGB value).
Are there any commands or functions available?

Thanks for your help!



Tue, 06 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps
  ========== in reply to ==========


  Date- Fri, 21 Aug 1998 14:32:08 -0400
  Subj- Retrieving Color Inormation of Bitmaps

/----begin quote-----
| How can I retrieve the color information of bitmaps by using REXX
| commands or functions. I'd like to read any bitmap pixel by pixel and
| check the color information (RGB value).
| Are there any commands or functions available?
\----end quote-----

You can use the REXX CHAR() function to read in the file.  Deciphering
the info, however, is something that will require you to know the
internal format of the image file you are reading.

   - r4 -

--
Cowboy Wisdom:
If you're ridin' ahead of the herd, take a look back every now and
then to make sure it's still there.
--------------------------------------------

The REXX Shop        http://www.rexxshop.com
Join our friendly maillist.  Send e-mail to

as the only word in the BODY of the mail.
--------------------------------------------
Try some free ICE Cream, our MR/2 ICE add-on
to automate and log your e-mail retrieval.



Tue, 06 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

>How can I retrieve the color information of bitmaps by using REXX
>commands or functions. I'd like to read any bitmap pixel by pixel and
>check the color information (RGB value).

Well there may or may not be a library available for this, but here is
something in plain Rexx.  It will input a BMP file and output a file
containing the following lines

P3
<width> <height>
255

and then a list of all the RGB values in the bitmap, in decimal.  [The
reason for the above three lines is that the resulting file will be in
PPM format which can be read by certain tools - the only problem is that
the image will be upside-down because BMP files are written with the bottom
line first and the top line last.]

The two arguments to the program are the input and output file names.

If you are wondering what the "msg" procedure is for, it's so you can
easily change the program to stop printing them or redirect them to a
file, not that there are many messages anyway.

I have tested it on a few BMPs and it seemed to work. :-)

signal on notready
parse arg in out

if charin(in,,2) \== 'BM' then call error 'input is not a BMP file'
call charin in,,8     /* skip size, hotspot */
bitoffset=getlong(in) /* start position in file of the bitmap data */
hdrlen=getlong(in)
select
   when hdrlen=12 then do   /* the BMP is in OS/2 format */
      win=0
      width=getshort(in)
      height=getshort(in)
      planes=getshort(in)
      bpp=getshort(in)
   end
   when hdrlen=40 then do   /* the BMP is in Windows format */
      win=1
      width=getlong(in)
      height=getlong(in)
      planes=getshort(in)
      bpp=getshort(in)
      call charin in,,24    /* don't know what these bytes are for... */
   end
   otherwise call error "Unrecognised header length:" hdrlen
end
pos=hdrlen+14               /* number of bytes read so far */
if win then type='Windows'; else type='OS/2'
call msg type "BMP," width"x"height"x"bpp"," planes "plane"||left("s",planes>1)

if planes\=1 then
   call error "Don't know how to read BMP with" planes "planes"
if bpp>8 & bpp\=16 & bpp\=24 then
   call error "Don't know how to read BMP with" bpp "bits per pixel"

if bpp<=8 then do           /* read the RGB table */
   ncols=2**bpp
   do i=0 for ncols
      blue.i=getbyte(in)
      green.i=getbyte(in)
      red.i=getbyte(in)
      if win then call charin in  /* alignment byte */
      pos=pos+3+win
   end
end
else ncols=0

if pos<bitoffset then do
   call msg 'Skipping' bitoffset-pos 'bytes...'
   call charin in,,bitoffset-pos
end
else if pos>bitoffset then
   call error 'Bit offset was' bitoffset '- should be at least' pos

call lineout out,'P3'
call lineout out,width height
call lineout out,255

linelen=(bpp*width+31)%32*4     /* bytes per line (must be multiple of 4) */

do height
   bits=x2b(c2x(charin(in,,linelen)))  /* read in a line in binary */
   do width
      parse var bits pixel +(bpp) bits /* parse off each pixel */
      select
         when bpp<=8 then do
            index=b2d(pixel)
            call lineout out,red.index green.index blue.index
         end
         when bpp=16 then do
            pixel=substr(pixel,9)||left(pixel,8)  /* reverse byte order */
            parse var pixel +1 red +5 green +5 blue +5
            call lineout out,b2d(red)*8 b2d(green)*8 b2d(blue)*8
         end
         when bpp=24 then do
            parse var pixel blue +8 green +8 red +8
            call lineout out,b2d(red) b2d(green) b2d(blue)
         end
      end
   end
end

exit

getlong: procedure
parse arg file
return c2d(reverse(charin(file,,4)))

getshort: procedure
parse arg file
return c2d(reverse(charin(file,,2)))

getbyte: procedure
parse arg file
return c2d(charin(file))

msg: procedure
parse arg text
say text
return

error:
parse arg text
call msg text
exit 1

notready:
stream=condition('D')
msg=stream(stream,'D')
call error 'NOTREADY condition on' stream':' msg
--

------ http://www.comlab.ox.ac.uk/oucl/users/ian.collier/imc.html

New to this group?  Answers to frequently-asked questions can be had from
http://rexx.hursley.ibm.com/rexx/ .



Fri, 09 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

>   It will input a BMP file

Ian:

Might you have a routine to pick out the width and height of an x-bitmap?

 --------------------------------------------------------------
 Dick Goran             author, REXX Reference Summary Handbook
 C F S Nevada, Inc.     Contrib. Ed., Personal Systems Magazine
 953 E. Sahara Ave, Suite 9B                     [OS/2 Advisor]
 Las Vegas, NV 89104-3012

 702-732-3847 FAX                                    (remove ?)
                    http://www.cfsrexx.com



Fri, 09 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

>Might you have a routine to pick out the width and height of an x-bitmap?

I don't just have one, but if you honestly mean an X11 bitmap then the header
looks like this:

/* optional comment */

#define foo_width 100
#define foo_height 64

so it really wouldn't be that hard.  Just read lines until you find the
"#define".
--

------ http://www.comlab.ox.ac.uk/oucl/users/ian.collier/imc.html

New to this group?  Answers to frequently-asked questions can be had from
http://rexx.hursley.ibm.com/rexx/ .



Fri, 09 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

 MK> How can I retrieve the color information of bitmaps by using
 MK> REXX commands or functions.

You'll have to know the file structure of a bitmap file (OS/2 BMP
and/or Windows BMP). With CharIn(), and C2D(), you should be able
to retrieve byte values. FWIW, there is a REXX application (check
Hobbes for it), called GRFXRX or something like that, which could
give you an idea.



Fri, 09 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

> but if you honestly mean an X11 bitmap then the header
> looks like this:

I was referring to the x-bitmap that is processed internally by Navigator
and Internet Explorer. I have never seen one so I'm not sure specifically
what x-bitmap refers to.

 --------------------------------------------------------------
 Dick Goran             author, REXX Reference Summary Handbook
 C F S Nevada, Inc.     Contrib. Ed., Personal Systems Magazine
 953 E. Sahara Ave, Suite 9B                     [OS/2 Advisor]
 Las Vegas, NV 89104-3012

 702-732-3847 FAX                                    (remove ?)
                    http://www.cfsrexx.com



Sun, 11 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:


>> but if you honestly mean an X11 bitmap then the header
>> looks like this:
>I was referring to the x-bitmap that is processed internally by Navigator
>and Internet Explorer.

If you mean the one which appears in mime.types as

image/x-xbitmap                 xbm

then that is indeed an X11 bitmap.  There is an example of one at
http://www-dsed.llnl.gov/documents/a_image.xbm (which was the merely
the first in a list of candidates resulting from a search at AltaVista).

Quote:
>                       I have never seen one so I'm not sure specifically
>what x-bitmap refers to.

One might ask the question, if you've never seen one then why do you need
a program which extracts information from one?
--

------ http://www.comlab.ox.ac.uk/oucl/users/ian.collier/imc.html

New to this group?  Answers to frequently-asked questions can be had from
http://rexx.hursley.ibm.com/rexx/ .



Sun, 11 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

> If you mean the one which appears in mime.types as

Thanks, but I have samples. I was wondering if you had a routine for picking
out width and height.

For others that were interested in GIFs and JPEGs, here are a couple of code
fragments:

 /*----------------------------------------------*\
 |  Get the width and height from the image file  |
 \*----------------------------------------------*/
 image_area = CHARIN( image_file, 1, CHARS( image_file ) )
 call STREAM image_file, 'C', 'CLOSE'

 select
    when POS( 'JFIF', image_area ) > 0 then
       do
          ffc0_ptr = LASTPOS( 'FFC0'x, image_area )
          if ffc0_ptr > 0 then
             do
                parse value image_area with,
                   =(ffc0_ptr) +5,
                   c_height +2,
                   c_width +2
                image_width  = C2D( c_width )
                image_height = C2D( c_height )
             end
       end
    when LEFT( image_area, 3 ) = 'GIF' then
       do
          parse value image_area with,
              7 w2,
             +1 w1,
             +1 h2,
             +1 h1,
             +1
          image_width  = C2D( w1 || w2 )
          image_height = C2D( h1 || h2 )
       end
    otherwise
       do
          image_width  = 'n/a'
          image_height = 'n/a'
       end
 end

 --------------------------------------------------------------
 Dick Goran             author, REXX Reference Summary Handbook
 C F S Nevada, Inc.     Contrib. Ed., Personal Systems Magazine
 953 E. Sahara Ave, Suite 9B                     [OS/2 Advisor]
 Las Vegas, NV 89104-3012

 702-732-3847 FAX                                    (remove ?)
                    http://www.cfsrexx.com



Sun, 11 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

>Thanks, but I have samples. I was wondering if you had a routine for picking
>out width and height.

Well now I'm confused, since you just told me you had never seen one.
But if you have samples then you can look at one and verify whether
it looks like

/* optional comment */

#define foo_width 444
#define foo_height 111

and if it does then the routine you want is almost trivial to write.
If it doesn't then I need more information about the file so I can
determine what type of image it is.
--

------ http://www.comlab.ox.ac.uk/oucl/users/ian.collier/imc.html

New to this group?  Answers to frequently-asked questions can be had from
http://rexx.hursley.ibm.com/rexx/ .



Sun, 11 Feb 2001 03:00:00 GMT  
 Retrieving Color Inormation of Bitmaps

Quote:

> Well now I'm confused, since you just told me you had never seen one.
> But if you have samples then you can look at one and verify whether
> it looks like

Sorry, what I intended to say (the days have been VERY long) was that I had
never worked with them before. One of the graphics packages I have is
distributed with samples of many different formats.

I only asked if you happened to have a code fragment readily available. I'll
find the specs for it somewhere and dig out the specifics.

Thanks anyway.

 --------------------------------------------------------------
 Dick Goran             author, REXX Reference Summary Handbook
 C F S Nevada, Inc.     Contrib. Ed., Personal Systems Magazine
 953 E. Sahara Ave, Suite 9B                     [OS/2 Advisor]
 Las Vegas, NV 89104-3012

 702-732-3847 FAX                                    (remove ?)
                    http://www.cfsrexx.com



Mon, 12 Feb 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Bitmaps and 256 colors

2. Bitmaps with different color depths

3. Retrieving Color Information

4. Setting color/pattern of disabled button bitmaps?

5. getting colors from bitmaps

6. Color Bitmaps

7. TCL QUESTIONS get bitmaps name or polygon color...

8. colored bitmaps

9. Colored bitmaps in Tcl/Tk

10. Color-Bitmaps in TK?

11. Q:Color bitmaps & GIFs

12. Creating color bitmaps

 

 
Powered by phpBB® Forum Software