For instance, let's say I have two images preferably jpeg/bmp that are 128x128 - I would like the code to stitch them together so the resulting image is 256x128.
I want a c++ program for it..is it possible using files in c++.

Dani AI

Generated

As pointed out, uncompressed BMP is the easiest format to work with for this task: no JPEG decoding is required, so the program only needs to read headers, copy pixel rows, and write a new header + pixel data. Given the original posts (128x128 inputs into a 256x128 output) the simplest, safest plan is to target 24‑bit BMPs and work in binary mode with a modern compiler (Turbo C is too old for convenient file and library support).

Basic workflow (concise):

  • Open both input BMPs in "rb" and the output in "wb". Read the 14‑byte BITMAPFILEHEADER and the 40‑byte BITMAPINFOHEADER from each input and validate bfType == 'BM', biCompression == 0 and biBitCount == 24 (or adapt bytes-per-pixel if different).
  • Compute outWidth = w1 + w2, outHeight = h (require same height or choose how to vertically align/pad).
  • Compute row padding: pad = (4 - (width * bytesPerPixel) % 4) % 4. Update the output headers: biWidth, biSizeImage = (outWidth*bytesPerPixel + outPad) * abs(outHeight), and bfSize = biSizeImage + sizeof(headers).
  • Copy rows scanline-by-scanline (remember BMP usually stores rows bottom-up when height > 0). For each row: read left row + skip left pad, read right row + skip right pad, write left+right to output and write output pad.

Minimal C-style sketch (illustrative; error checks omitted):

int padOf(int w,int bpp){ return (4 - (w*(bpp/8))%4) % 4; }

int pad1 = padOf(w1,bpp), pad2 = padOf(w2,bpp), outPad = padOf(w1+w2,bpp);
for(int row=0; row<height; ++row){
  fread(buf1,1,w1*bytesPerPixel,fin1); fseek(fin1,pad1,SEEK_CUR);
  fread(buf2,1,w2*bytesPerPixel,fin2); fseek(fin2,pad2,SEEK_CUR);
  fwrite(buf1,1,w1*bytesPerPixel,fout);
  fwrite(buf2,1,w2*bytesPerPixel,fout);
  fwrite(zeroBuf,1,outPad,fout);
}

Notes and cautions: handle negative biHeight (top-down BMPs), confirm biCompression==0, and open files in binary mode. For JPEG inputs, avoid reimplementing JPEG: use a decoder like a single-header image library or OpenCV to load into raw pixels, then combine and write BMP/JPEG. Finally, prefer a current Windows toolchain (MinGW‑w64, Visual Studio Community) so standard file APIs and libraries are available.

Recommended Answers

All 3 Replies

Sure it's possible.

My questions
- how much C++ do you know to begin with?
- which OS/Compiler are you using?
- which graphics library are you using?

- SCHOOL LEVEL( 11th and 12TH std.) and I am not very good
with files but this program needs to be made using files

- WINDOWS/ TC COMPILER

- graphics.h but are you sure that a graphics library
would be needed

http://en.wikipedia.org/wiki/BMP_file_format
Reading BMP files is easy. They're little more than a dump of the pixels.
Start simple - write a program which just prints out the width and height of any BMP file you give it.

> but are you sure that a graphics library would be needed
For JPG yes.
If you want to decode JPG from scratch, you need a lot more experience, probably university level maths and a lot of time to spare.

I might suggest you get a compiler that isn't older than you are. TC effectively became obsolete 20 years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.