You are not logged in.

#1 2009-04-01 05:27:14

vkumar
Member
Registered: 2008-10-06
Posts: 166

question on good c++ design for some code

I'm about to jump into a lung cancer project at the university, and I will need to modify some C++ code I have. I'm wondering what the best way to do it is. To elaborate;

// generalization of what I have now
#include "cvp_bmp.hpp"

// stuff

BMP slice;
slice.read("001.bmp");

// stuff

slice.write("final.001.bmp");

This is what the code will have to do;

// what I want
#include "cvp_image.hpp"

Image slice;
slice.read("001", "bmp");

slice.write("001", "pgm");

Image::read and Image::write need to support bmp, pgm, tiff, dacom, png, and more formats smile. The one thing all of the data structures will have in common is something like this;

struct img_data {
    ushort Pixels[512][512];
    uint histogram[256];
}

Is it a good idea to make separate classes for each format (each with their own static "read" and "write" functions), and have an Image class feed them img_data structs to get started? Is this good, efficient, design? (Keep in mind, this program will be processing hundreds of images simultaneously! Efficiency is very important!)

I've thought of some other designs, but this one seems to be the most feasible..

Last edited by vkumar (2009-04-01 05:28:00)


div curl F = 0

Offline

#2 2009-04-01 07:04:56

jsgt
Member
Registered: 2009-03-16
Posts: 29

Re: question on good c++ design for some code

The efficiency problem will likely depend more on the image encoding algorithms... the overhead of design will likely be minimal. I would create an Image class with pure virtual functions read and write and then derive a class for each format. Then you can for example do the following:

Image imgIn = new ImagePng( );
Image imgOut = new ImageBmp( );

imgIn.read( file );
imgOut.write( imgIn );

Where write takes an Image as a parameter and read a file. You could also make read a function implemented in Image that reads chunks from the file and feeds them to a virtual function.

Last edited by jsgt (2009-04-01 07:08:49)

Offline

#3 2009-04-01 19:10:25

vkumar
Member
Registered: 2008-10-06
Posts: 166

Re: question on good c++ design for some code

I think the design you have suggested will be better (read: simpler) to code. I think like I'm coding in C all of the time.. bad habit..

You could also make read a function implemented in Image that reads chunks from the file and feeds them to a virtual function.

There are some times ambiguous cases, and it would be tricky to know when to stop reading from the file and delegate the task to another class.. Also, other people have written classes for other formats, and afaik their code assumes that the file will be untouched. I'll probably determine which image class to use by checking the file extension (crude, but should work 99% of the time =/).


div curl F = 0

Offline

Board footer

Powered by FluxBB