#include <graphics.h>
#include <jpeglib.h>
void displayJPG(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
printf("Error al abrir el archivo JPG\n");
return;
}
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, file);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
int width = cinfo.output_width;
int height = cinfo.output_height;
int numChannels = cinfo.output_components;
unsigned char* buffer = (unsigned char*)malloc(width * height * numChannels);
unsigned char* rowPtr;
while (cinfo.output_scanline < height) {
rowPtr = buffer + (cinfo.output_scanline) * width * numChannels;
jpeg_read_scanlines(&cinfo, &rowPtr, 1);
}
closegraph();
initgraph(&gd, &gm, "C:\\TC\\BGI");
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = buffer[(y * width + x) * numChannels];
int g = buffer[(y * width + x) * numChannels + 1];
int b = buffer[(y * width + x) * numChannels + 2];
putpixel(x, y, COLOR(r, g, b));
}
}
free(buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
displayJPG("imagen.jpg");
getch();
closegraph();
return 0;
}