Почему неккоректный вывод массива в консоли Visual C++?

Добрый вечер.
Стоит задача: имеющееся изображение в формате jpeg, перевести в DIB и построить двумерную матрицу на основе этого DIB файла.

Использую Intel Jpeg library 1.5.
Программа выполняется корректно(проверял каждый кусок принтами).
Но при вызове массива, который получился. У меня выводит при таком выводе:
cout << img[n] << setw(5);
В консоли, вместо данных из массива - появляется масть "черва"(тобишь сердечко).
При вызове таким вот варварским способом:
cout << img << setw(5);
Выводит набор из мастей, несколько пик, тефы, червы.

Всю голову сломал над этим. Что это может быть. img - буфер с данными
задается так:
unsigned char *img = new unsigned char[ImageSize];
image.DIBBytes = img;

#include "stdafx.h"
//#include "scan.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <wchar.h>
#include <math.h>
#include <iostream>
#include "ijl.h"
#include <conio.h>
#include <windows.h> 
#include <dos.h> 
#pragma comment(lib,"ijl15.lib")

using namespace std;
int JPGquality = 75;//in percents
int i;
int j;

#define MAX_LOADSTRING 100

BOOL DecodeJPGFileToGeneralBuffer(
	LPCSTR lpszPathName,
	DWORD* width,
	DWORD* height,
	DWORD* nchannels,
	BYTE** buffer)
{
	BOOL bres;
	IJLERR jerr;
	DWORD x = 0; // pixels in scan line
	DWORD y = 0; // number of scan lines
	DWORD c = 0; // number of channels
	DWORD wholeimagesize;
	BYTE* pixel_buf = NULL;
	// Allocate the IJL JPEG_CORE_PROPERTIES structure.
	JPEG_CORE_PROPERTIES jcprops;
	bres = TRUE;
	__try
	{
		// Initialize the IntelR JPEG Library.
		jerr = ijlInit(&jcprops);
		if (IJL_OK != jerr)
		{
			bres = FALSE;
			__leave;
		}
		// Get information on the JPEG image
		// (i.e., width, height, and channels).
		jcprops.JPGFile = const_cast<LPSTR>(lpszPathName);
		jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
		if (IJL_OK != jerr)
		{
			bres = FALSE;
			__leave;
		}
		// Set up local data.
		x = jcprops.JPGWidth;
		y = jcprops.JPGHeight;
		c = 3; // Decode into a 3 channel pixel buffer.
			   // Compute size of desired pixel buffer.
		wholeimagesize = (x * y * c);
		// Allocate memory to hold the decompressed image data.
		pixel_buf = new BYTE[wholeimagesize];
		if (NULL == pixel_buf)
		{
			bres = FALSE;
			__leave;
		}
		// Set up the info on the desired DIB properties.
		jcprops.DIBWidth = x;
		jcprops.DIBHeight = y; // Implies a bottom-up DIB.
		jcprops.DIBChannels = c;
		jcprops.DIBColor = IJL_BGR;
		jcprops.DIBPadBytes = 0;
		jcprops.DIBBytes = pixel_buf;
		// Set the JPG color space ... this will always be
		// somewhat of an educated guess at best because JPEG
		// is "color blind" (i.e., nothing in the bit stream
		// tells you what color space the data was encoded from).
		// However, in this example we assume that we are
		// reading JFIF files which means that 3 channel images
		// are in the YCbCr color space and 1 channel images are
		// in the Y color space.
		switch (jcprops.JPGChannels)
		{
		case 1:
		{
			jcprops.JPGColor = IJL_G;
			break;
		}
		case 3:
		{
			jcprops.JPGColor = IJL_YCBCR;
			break;
		}
		default:
		{
			// This catches everything else, but no
			// color twist will be performed by the IJL.
			jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
			jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
			break;
		}
		}
		// Now get the actual JPEG image data into the pixel buffer.
		jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
		if (IJL_OK != jerr)
		{
			bres = FALSE;
			__leave;
		}
	} // __try
	__finally
	{
		if (FALSE == bres)
		{
			if (NULL != pixel_buf)
			{
				delete[] pixel_buf;
				pixel_buf = NULL;
			}
		}
		// Clean up the IntelR JPEG Library.
		ijlFree(&jcprops);
		*width = x;
		*height = y;
		*nchannels = c;
		*buffer = pixel_buf;
	} // __finally
	return bres;
} // DecodeJPGFileToGeneralBuffer()

DWORD x;
DWORD y;
DWORD nchannels;
BYTE* buffer;
char temp[255];

LPCSTR lpszPathName;

int main()
{
	

		JPEG_CORE_PROPERTIES jcprops;
		IJLERR jerr;
		int st, sty, l, i1, i2, j1, j2, kk;
		float mx, my, mxi, myi, ax, ay, a, m20, m02, m11, alpha, elongation;//разные моменты и центр масс
		sty = 0; y = 0;
		
		

			jerr = ijlInit(&jcprops);
			sprintf(temp, "0.jpg", 1);//MessageBoxA(0,temp,0,0);
			jcprops.JPGFile = const_cast<LPSTR>(temp);

			sty += y;

			jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
			x = jcprops.JPGWidth;
			y = jcprops.JPGHeight;
			ijlFree(&jcprops);
			buffer = new BYTE[x*y * 3];



			DecodeJPGFileToGeneralBuffer(temp,
				&x,
				&y,
				&nchannels,
				&buffer);

			std::cout << y << "=   os y" << endl;
			std::cout << x << "=   os x" << endl;
			std::cout << nchannels << "=   channel" << endl;
			std::cout << buffer[j*x * 3] << "=   buffer 1" << endl;
			std::cout << buffer[j*x * 3+1] << "=   buffer 2" << endl;
			std::cout << buffer[j*x * 3+1] << "=   buffer 3" << endl;
			system("pause");
			std::cout << "end"<< endl;
			system("pause");
}


Полностью переделал код программы.
Разузнал что данный буфер, представляет собой побайтно DIB файл.
Остался последний этап, перевести этот DIB файл в массив вида:
| 0 , 0 , 0 , 255|
| 0 , 0 ,255, 0 |
| 0 ,255, 0 , 0 |
|255, 0 , 0 , 0 |
  • Вопрос задан
  • 478 просмотров
Пригласить эксперта
Ответы на вопрос 2
AtomKrieg
@AtomKrieg
Давай я поищу в Google за тебя
cout << static_cast<uint8_t>(img[n]) << setw(5);
Ответ написан
Вы выводите char* и char, выводится текст до 0(если мне не изменяет память) и символ.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы