Related
I'm trying to apply the u-law algorithm to a wav file file.wav, and then create a new file file2.wav.
file.wav has 16 bits/sample, and I want to obtain a file2.wav that has 8 bits/sample.
This is my code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
using std::string;
using std::fstream;
typedef struct WAV_HEADER {
char RIFF[4];
unsigned long ChunkSize;
char WAVE[4];
char fmt[4];
unsigned long Subchunk1Size;
unsigned short AudioFormat;
unsigned short NumOfChan;
unsigned long SamplesPerSec;
unsigned long bytesPerSec;
unsigned short blockAlign;
unsigned short bitsPerSample;
char Subchunk2ID[4];
unsigned long Subchunk2Size;
} wav_hdr;
int headerSize = 0;
string path = "file.wav";
wav_hdr wavHeader;
FILE* openFile() {
const char* filePath;
FILE *wavFile;
headerSize = sizeof(wav_hdr);
filePath = path.c_str();
wavFile = fopen(filePath, "rb");
if (wavFile == NULL) {
printf("Error\n");
}
fread(&wavHeader, headerSize, 1, wavFile);
return wavFile;
}
int8_t MuLaw_Encode(int16_t number)
{
const uint16_t MULAW_MAX = 0x1FFF;
const uint16_t MULAW_BIAS = 33;
uint16_t mask = 0x1000;
uint8_t sign = 0;
uint8_t position = 12;
uint8_t lsb = 0;
if (number < 0)
{
number = -number;
sign = 0x80;
}
number += MULAW_BIAS;
if (number > MULAW_MAX)
{
number = MULAW_MAX;
}
for (; ((number & mask) != mask && position >= 5); mask >>= 1, position--)
;
lsb = (number >> (position - 4)) & 0x0f;
return (~(sign | ((position - 5) << 4) | lsb));
}
int fileSize(FILE *file) {
int fileSize = 0;
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
return fileSize;
}
double bitsPerSample() {
double bitsPerE;
bitsPerE = wavHeader.bitsPerSample;
return bitsPerE;
}
int main() {
FILE *wavFile;
wavFile = openFile();
FILE* fptr2;
fptr2 = fopen("file2.wav", "wb");
int samples_count = fileSize(wavFile) / bitsPerSample();
short int *value = new short int[samples_count];
for (int16_t i = 0; i < samples_count; i++)
{
fread(&value[i], samples_count, 1, wavFile);
cout << value[i] << " "; // the output is in the attached picture
MuLaw_Encode(value[i]);
}
fwrite(value, sizeof(char), samples_count, fptr2);
return 0;
}
I took the u-law algorithm from here (2.1. µ-Law Compression (Encoding) Algorithm)
Am I doing something wrong? Because I obtain a corrupt file.
No header is ever written to the result file, so the first part of the data would get interpreted as a header, and it would be wrong. You can see in the file that it does not start with RIFFþR�WAVEfmt or something sufficiently similar.
The data written to the result file is value, the original data read from the input file, not the µ-law encoded data (which is only cout'ed and not saved).
The loop that reads the samples reads some wrong samples, because the computation of samples_count puts the current position back at the start, where the header is.
Complete C++/C newbie here. I recompiled gcz2tga (I did not make this code, just found it on the webs) into a debug .exe file using Visual Studio 2019. Everything works well until it gets to "split_images" and then the program spits out this error:
Debug Error!
Program: C:\Users\Harrison\source\repos\gcz2tga\Debug\gcz2tga.exe
abort() has been called
(Press Retry to debug the application)
When I hit Retry, the program closes. The code is set up like this:
/* gcz2tga.c: Slice up a directory full of GCZ (texture) files into TGA files.
*
* Credit goes to afwefwe for reverse-engineering the texture format
* and LZSS compression */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Assuming x86, usual endian crap is not accounted for */
typedef unsigned char u8_t;
typedef unsigned short u16_t;
typedef unsigned int u32_t;
struct image
{
unsigned int width, height;
u8_t *planes;
};
struct clip
{
short x, y, w, h;
};
static u16_t swab16(u16_t in)
{
/* GC headers are big-endian */
return ((in & 0xFF) << 8) | (in >> 8);
}
static void unpack_gc(struct image *out, u8_t *in, size_t out_sz)
{
unsigned int npixels;
unsigned int i;
unsigned int j;
u16_t *pixels;
u16_t *pheight;
u16_t *pwidth;
u16_t *pmagic;
u16_t pixel;
pmagic = (u16_t *) in;
pheight = (u16_t *) (in + 14);
pwidth = (u16_t *) (in + 12);
if (*pmagic != 0x4347) {
fprintf(stderr, "(FAIL: Invalid header)\n");
exit(EXIT_FAILURE);
}
/* Set up output image struct */
in += 24;
out->width = swab16(*pwidth);
out->height = swab16(*pheight);
out->planes = malloc(out->width * out->height * 4);
/* Clamp W/H (don't know why this is necessary but it is) */
out->width = out->width > 1024 ? 1024 : out->width;
out->height = out->height > 1024 ? 1024 : out->height;
fprintf(stderr, "(%dx%d)", out->width, out->height);
/* Unpack pixels */
pixels = (u16_t *) in;
npixels = out->width * out->height;
if (out_sz > npixels * 4) {
/* Deep image (i.e. 32-bit) */
memcpy(out->planes, pixels, npixels * 4);
} else {
/* Shallow image (i.e. 16-bit) */
for (i = 0, j = 0 ; i < npixels ; i++) {
pixel = pixels[i];
out->planes[j++] = ((pixel ) & 0x1F) << 3; /* B */
out->planes[j++] = ((pixel >> 5) & 0x1F) << 3; /* G */
out->planes[j++] = ((pixel >> 10) ) << 3; /* R */
out->planes[j++] = pixel & 0x8000 ? 0xFF : 0x00; /* A */
}
}
}
static u8_t *expand_lzss(u8_t *lzss, size_t *pout_sz)
{
static u8_t ring[0x1000];
unsigned int ring_pos = 0x0FEE;
unsigned int chunk_offset;
unsigned int chunk_length;
u32_t control_word = 1;
size_t length;
u8_t cmd1;
u8_t cmd2;
u8_t *out;
u8_t *pos;
u8_t *in;
/* Header = 32 bit unpacked file length */
length = *((u32_t *) lzss);
*pout_sz = length;
if (length > 8000000) {
fprintf(stderr, "(FAIL: Unreasonably large expanded size %d)\n",
length);
exit(EXIT_FAILURE);
}
out = malloc(length * 2); /* Seems to overrun */
pos = out;
in = lzss + 4;
while (length > 0) {
if (control_word == 1) {
/* Read a control byte */
control_word = 0x100 | *in++;
}
/* Decode a byte according to the current control byte bit */
if (control_word & 1) {
/* Straight copy */
*pos++ = *in;
ring[ring_pos] = *in++;
ring_pos = (ring_pos + 1) % 0x1000;
length--;
} else {
/* Reference to data in ring buffer */
cmd1 = *in++;
cmd2 = *in++;
chunk_length = (cmd2 & 0x0F) + 3;
chunk_offset = ((cmd2 & 0xF0) << 4) | cmd1;
for ( ; chunk_length > 0 ; chunk_length--) {
/* Copy historical data to output AND current ring pos */
*pos++ = ring[chunk_offset];
ring[ring_pos] = ring[chunk_offset];
/* Update counters */
chunk_offset = (chunk_offset + 1) % 0x1000;
ring_pos = (ring_pos + 1) % 0x1000;
length--;
}
}
/* Get next control bit */
control_word >>= 1;
}
return out;
}
static void readfile(const char *filename, u8_t **data, long *nbytes)
{
FILE *f;
f = fopen(filename, "rb");
if (f == NULL) abort();
fseek(f, 0, SEEK_END);
*nbytes = ftell(f);
fseek(f, 0, SEEK_SET);
*data = malloc(*nbytes);
fread(*data, *nbytes, 1, f);
fclose(f);
}
void put8(FILE *f, unsigned char val)
{
fwrite(&val, 1, 1, f);
}
void put16(FILE *f, unsigned short val)
{
fwrite(&val, 2, 1, f);
}
void split_images(const char *in_dir, const char *out_dir,
struct image *images, int nimages)
{
struct clip *clips;
char filename[512];
long nbytes;
u8_t *data;
char *name;
FILE *f;
int i;
int j;
int k;
/* Read file and get TOC */
sprintf(filename, "%s/system.idx", in_dir);
readfile(filename, &data, &nbytes);
clips = (struct clip *) (data + 0x01BC);
name = (char *) (data + 8 + *((long *) data));
/* Guess how many clips there are with a heuristic */
for (i = 0 ; clips[i].w != 0 && clips[i].h != 0 ; i++) {
sprintf(filename, "%s/%s.tga", out_dir, name);
name += strlen(name) + 3;
f = fopen(filename, "wb");
if (f == NULL) abort();
/* Locate the correct source image */
j = 0;
while (clips[i].y > images[j].height) {
clips[i].y -= images[j].height;
j++;
}
/* Write header */
put8(f, 0); put8(f, 0); put8(f, 2);
put16(f, 0); put16(f, 0); put8(f, 0);
put16(f, 0); put16(f, 0); put16(f, clips[i].w); put16(f, clips[i].h);
put8(f, 32); put8(f, 32);
/* Write scanlines */
for (k = 0 ; k < clips[i].h ; k++) {
if (clips[i].y == images[j].height) {
clips[i].y = 0;
j++;
}
fwrite(images[j].planes + ((images[j].width * clips[i].y) +
clips[i].x) * 4, clips[i].w, 4, f);
clips[i].y++;
}
/* Close output file */
fclose(f);
}
/* Cleanup */
free(data);
}
int main(int argc, char **argv)
{
char *in_dir;
char *out_dir;
struct image images[32];
char filename[256];
unsigned int i;
long filesize;
u8_t *lzss, *gc;
size_t out_sz;
FILE *f;
/* Usage */
if (argc != 3) {
fprintf(stderr, "Usage: %s [indir] [outdir]\n", argv[0]);
return EXIT_FAILURE;
}
/* Setup */
memset(images, 0, sizeof(images));
in_dir = argv[1];
out_dir = argv[2];
for (i = 0 ; i < 32 ; i++) {
/* Open 0.gcz, 1.gcz etc ... */
sprintf(filename, "%s/%d.gcz", in_dir, i);
f = fopen(filename, "rb");
if (f == NULL) break;
/* Read entire file */
fseek(f, 0, SEEK_END);
filesize = ftell(f);
fseek(f, 0, SEEK_SET);
fprintf(stderr, "%s: fread", filename);
lzss = malloc(filesize);
fread(lzss, filesize, 1, f);
fclose(f);
/* Decompress */
fprintf(stderr, "(OK) expand_lzss");
gc = expand_lzss(lzss, &out_sz);
free(lzss);
/* Unpack GC to 32-bit RGBA */
fprintf(stderr, "(OK) unpack_gc");
unpack_gc(&images[i], gc, out_sz);
free(gc);
fprintf(stderr, "(OK)\n");
}
/* Sanity check */
if (i == 0) {
fprintf(stderr, "No GCZ files found\n");
exit(EXIT_FAILURE);
}
/* Emit pile of TGAs */
fprintf(stderr, "split_images");
split_images(in_dir, out_dir, images, i);
fprintf(stderr, "(OK)\n\n");
return 0;
}
What is wrong with the code that could be causing this? The code is unaltered save for #define _CRT_SECURE_NO_WARNINGS being added to the code before the #include headers and having the program compiled as C.
I have a exercise. It says, that the C program should be able to read the information of a bitmap file and after that it should display the picture on console.
I have already written a code but when it does not work correctly.
When I debugged the code it looks like the heap is corrupted. I thinks I have a known glitch/mistake in ScanPixelline function.
I don't know how to fix it. Can someone help me to check it?
I am relatively new to C programming.
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
#include "windows.h"
#pragma pack(1)
struct BMP
{
char Type[2]; //File type. Set to "BM".
int32_t Size; //Size in BYTES of the file.
int16_t Reserved1; //Reserved. Set to zero.
int16_t Reserved2; //Reserved. Set to zero.
int32_t OffSet; //Offset to the data.
int32_t headsize; //Size of rest of header. Set to 40.
int32_t Width; //Width of bitmap in pixels.
int32_t Height; // Height of bitmap in pixels.
int16_t Planes; //Number of Planes. Set to 1.
int16_t BitsPerPixel; //Number of Bits per pixels.
int32_t Compression; //Compression. Usually set to 0.
int32_t SizeImage; //Size in bytes of the bitmap.
int32_t XPixelsPreMeter; //Horizontal pixels per meter.
int32_t YPixelsPreMeter; //Vertical pixels per meter.
int32_t ColorsUsed; //Number of colors used.
int32_t ColorsImportant; //Number of "important" colors.
};
struct Color
{
unsigned char B;
unsigned char G;
unsigned char R;
};
struct ColorTable
{
Color *colors;
unsigned long length;
};
struct PixelArray
{
Color **pixels;
unsigned long rowCount;
unsigned long columnCount;
};
void readBMP(char *File_Name, BMP &a)
{
FILE *p = fopen(File_Name, "rb");
if (p == NULL)
{
printf("Can't open file!");
fclose(p);
return;
}
else
{
fread(&a, sizeof(BMP), 1, p);
}
fclose(p);
}
void Get_Inf(BMP a)
{
if (a.Type[0] != 'B' || a.Type[1] != 'M')
{
printf("This is not a BMP file");
}
else
{
printf("This is a BMP file\n");
printf("The size of this file is %lu bytes\n", a.Size);
printf("The witdth of this image is %lu pixels\n", a.Width);
printf("The height of this image is %lu pixels\n", a.Height);
printf("The number of bits per pixels in this image is %u\n", a.BitsPerPixel);
}
}
void scanBmpPixelLine(Color *&line, unsigned long length)
{
FILE *pointer_ = fopen("test.bmp", "rb");
line = new Color[length];
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
fclose(pointer_);
//file.read((char *)line, length * sizeof(Color));
}
void skipBmpPadding(char count)
{
FILE *pointer__ = fopen("test.bmp", "rb");
if (count == 0)
{
fclose(pointer__);
return;
}
char padding[3];
fread(&padding, sizeof(char), count, pointer__);
fclose(pointer__);
//file.read((char *)&padding, count);
}
void ReadPixelArray(BMP a, PixelArray &data)
{
FILE *pointer = fopen("test.bmp", "rb");
data.rowCount = a.Height;
data.columnCount = a.Width;
data.pixels = new Color*[data.rowCount];
char paddingCount = (4 - (a.Width * (a.BitsPerPixel / 8) % 4)) % 4;
fseek(pointer, 54, SEEK_SET);
for (int i = 0; i < data.rowCount; i++)
{
scanBmpPixelLine(data.pixels[data.rowCount - i - 1], a.Width);
skipBmpPadding(paddingCount);
}
}
void drawBmp(BMP a, PixelArray data)
{
HWND console = GetConsoleWindow();
HDC hdc = GetDC(console);
for (int i = 0; i < a.Height; i++)
for (int j = 0; j < a.Width; j++)
{
Color pixel = data.pixels[i][j];
SetPixel(hdc, j, i, RGB(pixel.R, pixel.G, pixel.B));
}
ReleaseDC(console, hdc);
}
void releaseBmpPixelArray(PixelArray data)
{
for (int i = 0; i < data.rowCount; i++)
delete[]data.pixels[i];
delete[]data.pixels;
}
int main()
{
char file_name[] = "test.bmp";
BMP a;
PixelArray data;
readBMP(file_name, a);
Get_Inf(a);
ReadPixelArray(a, data);
drawBmp(a, data);
releaseBmpPixelArray(data);
}
This function:
void scanBmpPixelLine(Color *&line, unsigned long length)
{
FILE *pointer_ = fopen("test.bmp", "rb");
line = new Color[length];
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
fclose(pointer_);
//file.read((char *)line, length * sizeof(Color));
}
For starters, the intent of the function appears to be to read one line of pixel data from the file. But instead, it's re-opening the file and reading from the beginning (where the header bytes are). I'm not sure if you are aware of that...
But the crash is a result of this line:
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
The second parameter, sizeof(Color), is the size of each element. The third parameter is the number of elements to read. The total bytes read from the file will be the multiplication of the second parameter by the third parameter. So you've redundantly multiplied by sizeof(Color) one too many times. The result is that it will overwrite the line buffer.
To fix, it should be:
fread(line, sizeof(Color), length, pointer_);
You probably want to pass the FILE* pointer obtained from your ReadPixelArray function into this function instead of re-opening the file for every line.
Another code review comment. You should just read the entire file into memory instead of redundantly opening and closing the file for each operation. Then parse the header and set a pointer to the first "line" after the header.
Hy ,so my project to migrate to Visual Studio 2015. However I got stuck when I saw that receive 13 errors.
Error C2872 'data': ambiguous symbol Error C2872 'size': ambiguous symbol
Before you migrate to vss2015 everything work perfectly, I can not understand why we have moved the program after receiving this error.
Here is my Jpeg.cpp
#include "StdAfx.h"
#include "JpegFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <libjpeg-6b/jpeglib.h>
#include <libjpeg-6b/jpegLibLink.h>
#define OUTBUFFER_SIZE 0x8000
static FILE*fi;
static JOCTET * buffer;
static unsigned char*dest;
static int len;
static int destlen;
static unsigned char*data;
static int pos;
static int size;
static void file_init_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
if(!buffer) {
perror("malloc");
printf("Out of memory!\n");
exit(1);
}
dmgr->next_output_byte = buffer;
dmgr->free_in_buffer = OUTBUFFER_SIZE;
}
static boolean file_empty_output_buffer(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
if(fi)
fwrite(buffer, OUTBUFFER_SIZE, 1, fi);
dmgr->next_output_byte = buffer;
dmgr->free_in_buffer = OUTBUFFER_SIZE;
return 1;
}
static void file_term_destination(j_compress_ptr cinfo)
{ struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
if(fi)
fwrite(buffer, OUTBUFFER_SIZE-dmgr->free_in_buffer, 1, fi);
free(buffer);
buffer = 0;
dmgr->free_in_buffer = 0;
}
static void mem_init_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
dmgr->next_output_byte = dest;
dmgr->free_in_buffer = destlen;
}
static boolean mem_empty_output_buffer(j_compress_ptr cinfo)
{
printf("jpeg mem overflow!\n");
exit(1);
}
static void mem_term_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
len = destlen - dmgr->free_in_buffer;
dmgr->free_in_buffer = 0;
}
int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
if(filename) {
fi = fopen(filename, "wb");
if(fi == NULL)
return 0;
} else
fi = NULL;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
mgr.init_destination = file_init_destination;
mgr.empty_output_buffer = file_empty_output_buffer;
mgr.term_destination = file_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo,quality,TRUE);
//jpeg_write_tables(&cinfo);
//jpeg_suppress_tables(&cinfo, TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
if(fi)
fclose(fi);
jpeg_destroy_compress(&cinfo);
return 1;
}
int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*_fi)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
fi = _fi;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
mgr.init_destination = file_init_destination;
mgr.empty_output_buffer = file_empty_output_buffer;
mgr.term_destination = file_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_IFAST;
jpeg_set_quality(&cinfo,quality,TRUE);
//jpeg_write_tables(&cinfo);
//jpeg_suppress_tables(&cinfo, TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 1;
}
int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*_dest, int _destlen)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
dest = _dest;
len = 0;
destlen = _destlen;
mgr.init_destination = mem_init_destination;
mgr.empty_output_buffer = mem_empty_output_buffer;
mgr.term_destination = mem_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_IFAST;
jpeg_set_quality(&cinfo,quality,TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return len;
}
void mem_init_source (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
mgr->next_input_byte = data;
mgr->bytes_in_buffer = size;
//printf("init %d\n", size - mgr->bytes_in_buffer);
}
boolean mem_fill_input_buffer (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("fill %d\n", size - mgr->bytes_in_buffer);
return 0;
}
void mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("skip %d +%d\n", size - mgr->bytes_in_buffer, num_bytes);
if(num_bytes<=0)
return;
mgr->next_input_byte += num_bytes;
mgr->bytes_in_buffer -= num_bytes;
}
boolean mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("resync %d\n", size - mgr->bytes_in_buffer);
mgr->next_input_byte = data;
mgr->bytes_in_buffer = size;
return 1;
}
void mem_term_source (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
//printf("term %d\n", size - mgr->bytes_in_buffer);
}
int jpeg_load_from_mem(unsigned char*_data, int _size, unsigned char*dest, int width, int height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
struct jpeg_source_mgr mgr;
int y;
//int x;
data = _data;
size = _size;
jpeg_create_decompress(&cinfo);
mgr.next_input_byte = data;
mgr.bytes_in_buffer = size;
mgr.init_source =mem_init_source ;
mgr.fill_input_buffer =mem_fill_input_buffer ;
mgr.skip_input_data =mem_skip_input_data ;
mgr.resync_to_restart =mem_resync_to_restart ;
mgr.term_source =mem_term_source ;
cinfo.err = jpeg_std_error(&jerr);
cinfo.src = &mgr;
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
for(y=0;y<height;y++) {
unsigned char*j = &dest[width*y*3];
jpeg_read_scanlines(&cinfo,&j,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return 1;
}
typedef struct _RGBA {
unsigned char a,r,g,b;
} RGBA;
typedef unsigned char U8;
int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
//struct jpeg_source_mgr mgr;
FILE*fi = fopen(filename, "rb");
if(!fi) {
fprintf(stderr, "Couldn't open file %s\n", filename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fi);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
U8*scanline = (U8 *)malloc(4 * cinfo.output_width);
int width = *_width = cinfo.output_width;
int height = *_height = cinfo.output_height;
*dest = (unsigned char*)malloc(width*height*4);
int y;
for (y=0;y<height;y++) {
int x;
U8 *js = scanline;
RGBA*line = &((RGBA*)(*dest))[y*width];
jpeg_read_scanlines(&cinfo, &js, 1);
if (cinfo.out_color_space == JCS_GRAYSCALE) {
for (x = 0; x < width; x++) {
line[x].a = 255;
line[x].r = line[x].g = line[x].b = js[x];
}
} else if (cinfo.out_color_space == JCS_RGB) {
for (x = width - 1; x >= 0; x--) {
line[x].a = 255;
line[x].r = js[x*3+0];
line[x].g = js[x*3+1];
line[x].b = js[x*3+2];
}
} else if (cinfo.out_color_space == JCS_YCCK) {
fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
return 0;
} else if (cinfo.out_color_space == JCS_YCbCr) {
for (x = 0; x < width; x++) {
int y = js[x * 3 + 0];
int u = js[x * 3 + 1];
int v = js[x * 3 + 1];
line[x].a = 255;
line[x].r = y + ((360 * (v - 128)) >> 8);
line[x].g = y - ((88 * (u - 128) + 183 * (v - 128)) >> 8);
line[x].b = y + ((455 * (u - 128)) >> 8);
}
} else if (cinfo.out_color_space == JCS_CMYK) {
for (x = 0; x < width; x++) {
int white = 255 - js[x * 4 + 3];
line[x].a = 255;
line[x].r = white - ((js[x * 4] * white) >> 8);
line[x].g = white - ((js[x * 4 + 1] * white) >> 8);
line[x].b = white - ((js[x * 4 + 2] * white) >> 8);
}
}
}
free(scanline);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fi);
return 1;
}
And here is Jpeg.h
#include <stdio.h>
int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename);
int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*fi);
int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*dest, int destsize);
int jpeg_load(const char*filename, unsigned char**dest, int*width, int*height);
int jpeg_load_from_mem(unsigned char*_data, int size, unsigned char*dest, int width, int height);
I get the error for "data" and also for "size" symbol.
Best regards.
Why in visual studio 2013 i dont have that this error? And how to solve..
I get ambiguous symbol 10 times, in 10 sintax in jpeg.cpp
I am trying to write a function which will change all RGB(255,0,0) pixels to RGB(255,0,255) but it doesn't seem to work :D Could anyone tell me the reason why it doesn't work/show me the right way to do it. All criticism all appreciated.
Here is part of my code:
#include <stdio.h>
#include <Windows.h>
#include <stdint.h>
struct BmpSignature
{
unsigned char data[2];
};
#pragma pack(1)
struct BmpHeader
{
BmpSignature signature;
uint32_t fileSize;
uint16_t reserved1;
uint16_t reserved2;
uint32_t dataOffset;
};
struct BmpDib
{
uint32_t dibSize;
int32_t imageWidth;
int32_t imageHeight;
uint16_t colorPlaneCount;
uint16_t pixelSize;
uint32_t compressMethod;
uint32_t bitmapByteCount;
int32_t horizontalResolution;
int32_t verticalResolution;
uint32_t colorCount;
uint32_t importantColorCount;
};
struct Color
{
unsigned char blue;
unsigned char green;
unsigned char red;
};
struct PixelArray
{
Color **pixels;
uint32_t rowCount;
uint32_t columnCount;
};
void changeBmp(PixelArray &p)
{
for (int i = 0; i < p.rowCount; i++)
for (int j = 0; j < p.columnCount; j++)
{
if (p.pixels[i][j].red == 255 && p.pixels[i][j].green == 0 && p.pixels[i][j].blue == 0)
{
p.pixels[i][j].blue = 255;
}
}
}
void readBmpPixelArray(FILE *f, BmpHeader header, BmpDib dib, PixelArray &data)
{
if (f == NULL)
return;
data.rowCount = dib.imageHeight;
data.columnCount = dib.imageWidth;
data.pixels = new Color*[data.rowCount];
char paddingCount = (4 - (dib.imageWidth * (dib.pixelSize / 8) % 4)) % 4;
fseek(f, header.dataOffset, 0L);
for (int i = 0; i < data.rowCount; i++)
{
scanBmpPixelLine(f, data.pixels[data.rowCount - i - 1], dib.imageWidth);
skipBmpPadding(f, paddingCount);
}
}
void writeBmpPixelArray(FILE *f, BmpHeader header, BmpDib dib, PixelArray &data)
{
if (f == NULL)
return;
data.rowCount = dib.imageHeight;
data.columnCount = dib.imageWidth;
char paddingCount = (4 - (dib.imageWidth * (dib.pixelSize / 8) % 4)) % 4;
fseek(f, header.dataOffset, 0L);
for (int i = 0; i < data.rowCount; i++)
{
writeBmpPixelLine(f, data.pixels[data.rowCount - i - 1], dib.imageWidth);
addBmpPadding(f, paddingCount);
}
}
void writeBmpPixelLine(FILE *f, Color *&line, uint32_t length)
{
if (f == NULL)
return;
fwrite(line, sizeof(Color), length, f);
}
void scanBmpPixelLine(FILE *f, Color *&line, uint32_t length)
{
if (f == NULL)
return;
line = new Color[length];
fread(line, sizeof(Color), length, f);
}
void addBmpPadding(FILE *f, char count)
{
if (f == NULL)
return;
if (count == 0)
return;
char padding[3];
fwrite(padding, count, 1, f);
}
void skipBmpPadding(FILE *f, char count)
{
if (f == NULL)
return;
if (count == 0)
return;
char padding[3];
fread(padding, count, 1, f);
}
void drawBmp(BmpDib dib, PixelArray data)
{
HWND console = GetConsoleWindow();
HDC hdc = GetDC(console);
for (int i = 0; i < dib.imageHeight; i++)
for (int j = 0; j < dib.imageWidth; j++)
{
Color pixel = data.pixels[i][j];
SetPixel(hdc, j, i, RGB(pixel.red, pixel.green, pixel.blue));
}
ReleaseDC(console, hdc);
}
void releaseBmpPixelArray(PixelArray data)
{
for (int i = 0; i < data.rowCount; i++)
delete[]data.pixels[i];
delete[]data.pixels;
}
int main()
{
BmpHeader header;
BmpDib info;
PixelArray data;
FILE * inputBMP;
inputBMP = fopen("D:\\Projects\\bitmap\\yoyo.bmp", "rb");
readBmpHeader(inputBMP, header);
printBmpHeader(header);
readBmpDib(inputBMP, info);
printBmpDib(info);
readBmpPixelArray(inputBMP, header, info, data);
HWND console = GetConsoleWindow();
if (console != NULL){ MoveWindow(console, 0, 0, 800, 600, TRUE); }
changeBmp(data);
drawBmp(info, data);
releaseBmpPixelArray(data);
fclose(inputBMP);
Sleep(100);
return 0;
}