Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ON ERROR question
#4
Hmm... ok, thanks guys. I think I may have used ON ERROR a few times back in the GWBasic days so I wasn't aware of this.

It would be handy though if a label inside of a sub/function could be used. Take the code below for instance, it's the function I created from Steve's code in the other thread concerning gathering color depths. This function will fail if a negative arbitrary number is sent that does not belong to an actual image. I would like to catch that error when _PIXELSIZE encounters it and then simply exit the function, returning a value of 0. If I were to place the label outside of this function then other libraries loading before or after this would cause an error relating to code between subs/functions.

It would also be nice to test for a text mode only image being sent by copying the image and doing a simple PSET to it. If it fails then it's a text mode only image that was passed.

Code: (Select All)

'LIB_IMG_ColorDepth.BM

FUNCTION IMG_ColorDepth% (i AS LONG)

    '+------------------------------------------------+
    '| Returns the color depth of an image passed in. |
    '|                                                |
    '| i - the image handle                          |
    '|                                                |
    '| Returns:  0 : not an image                    |
    '|          32 : 32 bit color image              |
    '|          256 : 256 color image                |
    '|          16 : 16 color image (or text only)  |
    '|                                                |
    '| Note: this function will fail if an image      |
    '|      handle value passed in is less than -1  |
    '|      but does not belong to an actual image.  |
    '+------------------------------------------------+

    DIM c AS INTEGER ' color counter
    DIM p AS INTEGER ' palette counter

    IF i > -2 THEN EXIT FUNCTION '                                not an image, return 0
    IF _PIXELSIZE(i) = 4 THEN '                                  4 bytes per pixel?
        IMG_ColorDepth% = 32 '                                    yes, 32 bit color image
    ELSE '                                                        no, 1 byte per pixel
        p = 0 '                                                  reset palette counter
        c = 0 '                                                  reset color counter
        DO '                                                      begin palette search
            IF _PALETTECOLOR(p, i) <> &HFF000000 THEN c = c + 1 ' increment color counter if color found
            p = p + 1 '                                          increment palette counter
        LOOP UNTIL p = 256 '                                      leave when entire palette searched
        IF c <= 16 THEN '                                        16 colors or less?
            IMG_ColorDepth% = 16 '                                yes, 16 color image
        ELSE '                                                    no, greater than 16
            IMG_ColorDepth% = 256 '                              256 color image
        END IF
    END IF

END FUNCTION
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
ON ERROR question - by TerryRitchie - 08-16-2024, 07:16 PM
RE: ON ERROR question - by RhoSigma - 08-16-2024, 07:51 PM
RE: ON ERROR question - by Pete - 08-16-2024, 08:29 PM
RE: ON ERROR question - by TerryRitchie - 08-16-2024, 09:16 PM
RE: ON ERROR question - by Pete - 08-16-2024, 09:38 PM
RE: ON ERROR question - by TerryRitchie - 08-16-2024, 09:43 PM
RE: ON ERROR question - by Pete - 08-16-2024, 09:54 PM
RE: ON ERROR question - by TerryRitchie - 08-16-2024, 10:00 PM
RE: ON ERROR question - by Pete - 08-16-2024, 10:19 PM
RE: ON ERROR question - by SMcNeill - 08-16-2024, 10:41 PM
RE: ON ERROR question - by RhoSigma - 08-16-2024, 10:58 PM
RE: ON ERROR question - by TerryRitchie - 08-17-2024, 02:53 AM
RE: ON ERROR question - by RhoSigma - 08-17-2024, 02:10 PM
RE: ON ERROR question - by TerryRitchie - 08-17-2024, 06:59 PM
RE: ON ERROR question - by Pete - 08-16-2024, 11:23 PM
RE: ON ERROR question - by Pete - 08-17-2024, 04:24 AM
RE: ON ERROR question - by luke - 08-17-2024, 11:58 AM
RE: ON ERROR question - by eoredson - 09-02-2024, 04:39 AM
RE: ON ERROR question - by Pete - 08-19-2024, 03:15 AM



Users browsing this thread: 10 Guest(s)