Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:47:59+00:00 2026-06-12T14:47:59+00:00

I’m working with fits images using C++. This format can hold 8/16/32 bits integers

  • 0

I’m working with fits images using C++. This format can hold 8/16/32 bits integers and 32/64 bits floating point arrays. The pixel size (variable type) is contained given by a header flag.
When I read one of this images using cfitsio library [http://heasarc.gsfc.nasa.gov/fitsio/] I get a char array that holds all the pixels of the image:

char* array = new char[npix*bytepix]; // bytepix is the number of bytes per pixel
fits_read_img(infptr, datatype, first, npix, &nulval, lArray, &anynul, &status);

I can then get the “true” value of the pixel by a type cast. I.E. to get the value of the ith pixel for a 32 bits integer I would do:

int32_t pixelValue = ((int32_t*) lArray)[i];

I would like to know what is the most compact way of dealing with this in general, as I don’t know what the pixel type will be when I write the code.

What I currently do is something like this:

switch(bytepix){
    case 1:{
      int8_t *vecVal = ((int8_t*) array );
    }
      break;
    case 2:{
      int16_t *vecVal = ((int16_t*) array );
    }
      break;
    case 4:{
      int32_t *vecVal = ((int32_t*) array );
    }
      break;
    case 8:{
      int64_t *vecVal = ((int64_t*) array );
    }
      break;
    default:
      cout << "error\n";
      break;
  }
}

That is clearly ugly and not very flexible.

Thanks a lot for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:48:01+00:00Added an answer on June 12, 2026 at 2:48 pm

    What you’re trying to do is called generic programming. It’s a fundamental concept that splits the algorithm and the types it operates on. It is supported in C++ with template functions and template classes, and the whole C++ Standard Template Library is based upon it.

    Basically, implement your processing function independently of the FITS pixel size:

    template<typename Pixel>
    void ProcessImage(Pixel *array, const int arr_length) {
      ...
    }
    

    And then call it with the respective pixel size:

    ...
    case 1:
      ProcessImage<int8_t>(reinterpret_cast<int8_t *>(array), arr_length);
      break;
    case 2:
      ProcessImage<int16_t>(reinterpret_cast<int16_t *>(array), arr_length);
      break;
    ...
    

    A couple of thoughts:

    1. You use C++ but write C-style code. I suggest you get familiar with C++ STL, containers and algorithms, they’re exactly what is needed for image processing.
    2. There is a C++ CCfits alternative to the cfitsio library that might suit you better.
    3. There are libraries such as Boost::GIL that implement a lot of image processing algorithms. They’re heavily based on the templates and provide both complete functionality and good learning material.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.