PDA

View Full Version : uchar* to int



Wojtek_SZ
7th September 2012, 12:41
Hello,

How can I convert unsigned char* to int in Qt?

Santosh Reddy
7th September 2012, 13:09
one simple way
char* str = "2012";
int num = QString::fromLocal8Bit(str).toInt();

Wojtek_SZ
7th September 2012, 13:19
Thanks for reply, but Qt says "argument of type "uchar*" is incompatibile with parametr of "const uchar*"" :(
What can I do with my uchar*?

wyjyan
7th September 2012, 13:42
Yo

unsigned char *ptr = (unsigned char*)"S";
int num = (int)*ptr;

print ptr & num

spirit
7th September 2012, 14:13
See QString::toInt.


Yo

unsigned char *ptr = (unsigned char*)"S";
int num = (int)*ptr;

print ptr & num

I'm pretty sure that better to use C++ casting (http://www.stroustrup.com/bs_faq2.html#static-cast) than C ones.

Wojtek_SZ
7th September 2012, 14:18
I totally agree so I used:

uchar *poczatek;
poczatek = obraz.bits();
const char *adres = reinterpret_cast<const char*>(poczatek);
int j = QString::fromLocal8Bit(adres).toInt();

but I don't know why j = 0
both pointers as well...

spirit
7th September 2012, 14:22
Lets clarify: what are you going to achieve? Do you need to convert a number which contains in a string (e.g. "12" => 12) or what?

Wojtek_SZ
7th September 2012, 14:38
Sorry :)

I'm trying to

get adres of first pixel of obraz image so I could use j
as a starting point in loop:


for(int i = j; i <= obraz.height(); i++){
int licznik = 0;
for(int k = 1; k <= obraz.width(); k++){
QRgb tempColorRgb = obraz.pixel(k,i);
QColor tempColor = QColor(tempColorRgb);
wektor[i * obraz.width() + k + licznik] = tempColor.red();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.green();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.blue();
if (k == obraz.width())
k = 1;
}
}

spirit
7th September 2012, 14:49
And you want to do...? :)

Wojtek_SZ
1st October 2012, 12:50
Hello,


I've been very busy recently, I would like to back to my post:
I would like to send as a parrameter to my assembler function adress of first red data (each pixel of obraz image is split to RGB)
In order to achive this I have to convert uchar to int




void PROJ_ASM_SEPIA::odcien_szarosci_sl()
{
// int *wektor = new int[obraz.height()*obraz.width()*3];
uchar *poczatek;
poczatek = obraz.bits(); //getting adress of first pixel in image obraz (uchar)
const char *adres = reinterpret_cast<const char*>(poczatek); //convertion to const char
int j = QString::fromLocal8Bit(adres).toInt(); //convertion char to int

for(int i = j; i <= obraz.height(); i++){ //adres of first pixel (j) as a starting point of the loop
int licznik = 0;
for(int k = 1; k <= obraz.width(); k++){ //every pixel is split to RGB data - red green blue - data is stored in wektor
QRgb tempColorRgb = obraz.pixel(k,i);
QColor tempColor = QColor(tempColorRgb);
wektor[i * obraz.width() + k + licznik] = tempColor.red();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.green();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.blue();
if (k == obraz.width())
k = 1;
}
}


odcien_szar(wektor[0],obraz.width(),obraz.height()); //assembler function calling with adress of first data (red of first pixel)
}




The point is that the assembler function (gray scale) get adress of first pixel to make some calculations over rest of pixels.

Lesiok
1st October 2012, 15:42
Show the definition of the function odcien_szar

wysota
1st October 2012, 16:12
odcien_szar((int)wektor[0],obraz.width(),obraz.height());

provided that wektor[0] is the address you want to pass to odcien_szar. Note that your odcien_szar function should take a uchar* and not an int, though. sizeof(int) doesn't have to be equal to sizeof(uchar*).

Wojtek_SZ
1st October 2012, 18:17
Hello
here is the code of odcien_szar and sepia as well:





include filtry.inc

.code

;************************************************* ************************************************** ************************************************** ************************************************** ************************************************** ************
; ODCIEŃ SZAROŚCI

;************************************************* ************************************************** ************************************************** ************************************************** ************************************************** ************

pushad

odcien_szar PROC uses ebx edx, w_bmp :DWORD, szerokosc :DWORD, wysokosc :DWORD




mov eax, szerokosc ;ładowanie do rejestru szerokości obrazu.
mov ebx, wysokosc ;ładowanie do rejestru wysokości obrazu
mul ebx
xor ebx, ebx
mov ebx, 3 ;RGB - RED, GREEN, BLUE
mul ebx

mov esi, w_bmp ;wskaźnik do początku tablicy bajtów obrazu bitmapy.
mov ecx, eax ;ecx - liczba bajtów do przetworzenia

add ecx, esi

assume esi:ptr byte

petla: xor eax, eax
xor ebx, ebx

mov al, [esi] ;Å‚adowanie R
mov bl, [esi+1] ;Å‚adowanie G
add eax, ebx ;sumowanie R + G
mov bl, [esi+2] ;Å‚adowanie B
add eax, ebx ;sumowanie R + G + B
xor ebx, ebx
mov ebx, 3
div ebx
mov [esi], al ;zapisywanie R
mov [esi+1], al ;zapisywanie G
mov [esi+2], al ;zapisywanie B
add esi, ebx ;zwiększanie adresu
cmp ecx, esi ;porównywanie zawartości dwóch rejestrów
ja petla ;skocz na początek jeśli ecx > esi

popad
ret
odcien_szar endp

end

;************************************************* ************************************************** ************************************************** ************************************************** ************************************************** ************
; SEPIA

;************************************************* ************************************************** ************************************************** ************************************************** ************************************************** ************


pushad

sepia PROC uses eax ebx edx esi edi, w_bmp :DWORD, szerokosc :DWORD, wysokosc :DWORD


invoke odcien_szar, w_bmp, szerokosc, wysokosc ;wywołanie procedury


mov eax, szerokosc ;ładowanie do rejestru szerokości obrazu.
mov ebx, wysokosc ;ładowanie do rejestru wysokości obrazu
mul ebx
xor ebx, ebx
mov ebx, 3 ;RGB - RED, GREEN, BLUE
mul ebx

mov esi, w_bmp ;wskaźnik do początku tablicy bajtów obrazu bitmapy.
mov ecx, eax
add ecx, esi

assume esi:ptr byte

petla: xor eax, eax
xor ebx, ebx
;mov eax, parametr
mov ebx, 2
mul ebx
mov bl, [esi]
add eax, ebx
mov [esi], al

xor eax, eax
xor ebx, ebx
;mov eax, parametr
mov bl, [esi+1]
add eax, ebx
mov [esi+1], al

xor ebx, ebx
mov ebx, 3
add esi, ebx
cmp ecx, esi ;porównywanie zawartości dwóch rejestrów
ja petla ;skocz na początek jeśli ecx > esi

popad
ret
sepia endp
end











Wysota (Master of Zen) said:

Note that your odcien_szar function should take a uchar* and not an int, though. sizeof(int) doesn't have to be equal to sizeof(uchar*).



My intention is to send to function odcien_szar and sepia vector of binary representing data (RGB) so the further processing may take palce.

Lesiok
2nd October 2012, 11:01
Show a C header definition of this function not body.

Wojtek_SZ
2nd October 2012, 11:10
sorry c header of which function?
Spia and odcien_szar are functions stored in file with .asm extention, thus they hasn't got a c ext.

here is code of header PROJ_ASM_SEPIA class:



#ifndef PROJ_ASM_SEPIA_H
#define PROJ_ASM_SEPIA_H
#pragma once
#include <QtGui/QMainWindow>
#include <QMenu>
#include <qfiledialog.h>
#include <qmessagebox.h>
#include "ui_proj_asm_sepia.h"
#include <QBitmap>
#include <QLabel>
#include <QScrollArea>
#include<QString>





//#include "resource.h"
#include <windows.h>
extern "C" void _stdcall odcien_szar (DWORD x, DWORD y, DWORD z);
extern "C" void _stdcall sepia (DWORD x, DWORD y, DWORD z);

class PROJ_ASM_SEPIA : public QMainWindow
{
Q_OBJECT

public:
PROJ_ASM_SEPIA(QWidget *parent = 0, Qt::WFlags flags = 0);
~PROJ_ASM_SEPIA();
signals:

public slots:
void wyjscie();
void komunikat_o_programie();
void instrukcje();
void otworz_plik();
void odcien_szarosci_sl();
void sepia_sl();
private:
QImage obraz;
Ui::PROJ_ASM_SEPIAClass ui;
int konwersja(uchar *adres);
};

#endif // PROJ_ASM_SEPIA_H




here is the file tree of my vs solution
8271

Lesiok
2nd October 2012, 12:45
OK, first parameter (x) should be an address or value ?

Wojtek_SZ
2nd October 2012, 18:15
yes, exactly.

As the cell in memory are numbered by 0x values, I would like change this value to int (decimal) and sent the starting point of my vector to assembler function

wysota
2nd October 2012, 18:35
sent the starting point of my vector
That's an address, not a value.

Wojtek_SZ
2nd October 2012, 18:51
sorry I just lost myself....
Let' try from the other hand. How to send to function odcien_szar (acording to parameters of this function) whole information about every color rgb i every pixel?

wysota
2nd October 2012, 19:00
I would send QImage::bits() which is an address of a memory area containing pixel data for an image. Thus passing it as int albeit will succeed, is not a good thing to do because it is not a value but rather an address (aka pointer).

Wojtek_SZ
2nd October 2012, 19:15
This is my function:





void PROJ_ASM_SEPIA::odcien_szarosci_sl()
{
int *wektor = new int[obraz.height()*obraz.width()*3];
int j;
uchar *poczatek;
poczatek = obraz.bits();
//const char *adres = reinterpret_cast<const char*>(poczatek);
//int j = QString::fromLocal8Bit(adres).toInt();

//j = konwersja(poczatek);

for(int i = j; i <= obraz.height(); i++){
int licznik = 0;
for(int k = 1; k <= obraz.width(); k++){
QRgb tempColorRgb = obraz.pixel(k,i);
QColor tempColor = QColor(tempColorRgb);
wektor[i * obraz.width() + k + licznik] = tempColor.red();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.green();
licznik++;
wektor[i * obraz.width() + k + licznik] = tempColor.blue();
if (k == obraz.width())
k = 1;
}
}


odcien_szar(wektor[0],obraz.width(),obraz.height());



I am using exactly the same function bits() to get a pointer to first pixel data.
Now I want to create a map in memory of every pixel splitting everyone to rgb, so finaly vector will be 3 times bigger. to achive this I want also use pointer from function bits() converted to int as a starting point of the loop (var j) (process to RGB)
Please help I am despared:(

amleto
2nd October 2012, 20:16
int j; // what is j?
...
for(int i = j; ...) /// now what is i? OOPS




for(int k = 1; k <= obraz.width(); k++)
In c++ where arrays are zero-based, we start from 0, not 1. when k==obraz.width() you will go out of bounds.

Basic, basic c++. :confused:

wysota
2nd October 2012, 21:31
I'm having another doubt....

"wektor" is an array of integers (assuming 32 or 64 bits). To each cell (four or eight bytes) of the array you are assigning a colour component from a 24/32 bit image (making component values range from 0 to 255 which is 1 byte). Thus one thing is you are wasting 75% (in case of 32b ints) of the allocated memory and another is that you are probably (can't see it right now since we're on page 2 of this thread) not taking that into account when using the array in your assembly code. If you preview that array (by dumping it to screen or disk) you'll see 75% of the area is 0x00. And one more thing is that since you are working with long words, you have to take into consideration byte order (big-endian vs little-endian) of each long word.