how to retrieve texture information in DirectX? - directx

How can I retrieve the texture width, height, number of mipmap levels of the texture which has been already created in DirectX? what is the API for it?

Use ID3D10Texture2D::GetDesc, Assume pTexture is a valid pointer to ID3D10Texture2D, then
D3D10_TEXTURE2D_DESC texDesc;
pTexture->GetDesc(&texDesc);
Here is the information you can get from texDesc
typedef struct D3D10_TEXTURE2D_DESC {
UINT Width;
UINT Height;
UINT MipLevels;
UINT ArraySize;
DXGI_FORMAT Format;
DXGI_SAMPLE_DESC SampleDesc;
D3D10_USAGE Usage;
UINT BindFlags;
UINT CPUAccessFlags;
UINT MiscFlags;
} D3D10_TEXTURE2D_DESC;

Related

How can I get a window position (x,y) created by using OpenCV?

How can I get a window position (x,y) created by using OpenCV?
I can create a named window: namedWindow("my window");
I can move this window: moveWindow("my window", x, y);
But how can I get current position's coordinates of this window?
Also, there is function void loadWindowParameters("my window");, which can as written:
loads size, location, flags, trackbars value, zoom and panning
location of the window window_name
But does it load these parameters to where? Declaration of this function does not return anything - get only window name and return void.
Until the feature request for this feature gets done, if you need this functionality and can't wait, you can implement it yourself. You'll need the opencv source code, then you'll have to edit some opencv files and rebuild part of opencv.
I followed the source code for moveWindow() as a model.
That's what I did:
add to opencv/sources/modules/highgui/source/window_w32.cpp this function (I added it just below the cvMoveWindow definition):
CV_IMPL void cvGetWindowRect( const char* name, int &x, int &y, int &width, int &height)
{
CV_FUNCNAME( "cvGetWindowRect" );
__BEGIN__;
CvWindow* window;
RECT rect;
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name" );
window = icvFindWindowByName(name);
if(!window)
EXIT;
GetWindowRect( window->frame, &rect );
x = rect.left;
y = rect.top;
width = rect.right - rect.left;
height = rect.bottom - rect.top;
__END__;
}
and add its declaration in opencv/sources/modules/highgui/include/opencv2/highgui_c.h :
CVAPI(void) cvGetWindowRect( const char* name, int &x, int &y, int &width, int &height);
This alone would allow you to use the cvGetWindowRect from C/C++ to get the window rect. But if you want to use the C++ interface or the python interface (as I do) you can edit two files more:
add to opencv/sources/modules/highgui/source/window.cpp this function:
void cv::getWindowRect( const String& winname, CV_OUT int &x, CV_OUT int &y, CV_OUT int &width, CV_OUT int &height)
{
cvGetWindowRect(winname.c_str(), x, y, width, height);
}
and add its declaration in opencv/sources/modules/highgui/include/opencv2/highgui.hpp :
CV_EXPORTS_W void getWindowRect( const String& winname, CV_OUT int &x, CV_OUT int &y, CV_OUT int &width, CV_OUT int &height);
Then you'll have to rebuild the opencv_highgui project (I do this for windows with Visual Studio 2015). If you need the python bindings then rebuild the opencv_python3 project too. The CV_EXPORTS_W and CV_OUT macros are needed to expose the function and recognize the output parameters when building the python bindings. From python you'll get a 4-tuple as return value, ->eg:
>>> cv2.getWindowRect("my window")
(1024, 0, 817, 639)
For the python bindings you'll have to copy the new cv2.cp35-win_amd64.pyd and opencv_highgui300.dll to the PythonEnv\Lib\site-packages.

strange cvSaveImage behavior

i have recently started using opencv and this one has baffled me.
void saveImageSnippet(char *imageName, int height, int width, void* data, int nChannels) //data is char[height * width]
{
char fName[200]="c:\\testimg\\";
FILE *fptr;
IplImage *img;
sprintf(fName,"%s%s.bmp",fName,imageName);
img = cvCreateImageHeader(cvSize(width, height),8/*depth*/,nChannels);
img->imageData=(unsigned char*)data;
cvSaveImage(fName, img); //Unhandled exception
cvReleaseImage(&img);
}
At cvSaveImage : Unhandled exception at 0x6e8e871d in vc2008_1x.exe: 0xC0000005: Access violation reading location 0x745c3a63.
Is there anything i am not doing right?
now an interesting part,
if i add a couple of unused variables, cvSaveImage works just fine
void saveImageSnippet(char *imageName, int height, int width, void* data, int nChannels)
{
int var1, var2; //unused variables
char fName[200]="c:\\testimg\\";
FILE *fptr;
IplImage *img;
sprintf(fName,"%s%s.bmp",fName,imageName);
img = cvCreateImageHeader(cvSize(width, height),8/*depth*/,nChannels);
img->imageData=(unsigned char*)data;
cvSaveImage(fName, img); //now it works fine
cvReleaseImage(&img);
}
please use opencv's c++ api,
avoid anything, that has iplimages in it (especially here on SO !)
#include "opencv2/highgui.hpp" // no, *not* highgui.h !
using namespace cv;
int main()
{
string fName = format("c:\\testimg\\%s.bmp",imageName);
Mat img = imread( fName );
if ( img.empty() )
return -1;
// your image was not loaded
imsave(fName);
return 0;
}

GLSL: binding Shader Storage Buffer Object

I'm binding a Shader Storage Buffer Object (SSBO) and use in the Fragment Shader. The bindings are fine, the buffer block is a multiple of vec4(4B) blocks as required by the specifications. However, the following code has an unexpected behavior. The variable 'datapoint' has the correct 'a' value, but the 'b,c,d' values are garbage.
I'm using an Nvidia Quadro K5000 with driver version 340.52.
struct data
{
int a;
float b;
float c;
float d;
};
// the same location '0' is used on the host
layout(std430, binding = 0) buffer MyBuffer
{
data mydata[];
}
data datapoint;
void main()
{
...
datapoint = mydata[some_index]; // some_index is withing range
...
}

Explanation of IplImage* img

I just started learning OpenCV with "Learning OpenCV Computer Vision with the OpenCV Library".
In the first example which demonstrates how to display a picture, it has a line
IplImage* img = cvLoadImage("name.type")
Although the book explains it, I still do not fully know what exactly IplImage* img does.
Does cvLoadImage loads the image to img which IplImage is pointing to? Can anyone explain this to me please? Thank you
img is the name of the variable, might as well be blahblahblah;
IplImage is the type of the variable, it's just a struct that contains the image data itself plus some info (size, color depth, etc.) on the image;
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
For more info on IplImage: Other question about IplImage
cvLoadImage provides a pointer to an IplImage, which means it creates an IplImage when it loads it and returns you it's emplacement.
Do not forget to do cvReleaseImage(&img) when you are finished with it, if you do not want to have memory leaks.

where have the shape drawing functions gone in DirectX10?

such as the CreateTeapot function - http://msdn.microsoft.com/en-us/library/windows/desktop/bb172798(v=vs.85).aspx
Is there an equivalent in DX10? If so, how do I use it?
In DX9 you..
Declared:
LPD3DXMESH meshTeapot;
Initialised:
D3DXCreateTeapot(device, &meshTeapot, NULL);
Drew:
meshTeapot->DrawSubset(0);
Released:
meshTeapot->Release();
Is there an equivalent set of methods for drawing primitives? (to be honest the sphere is of more interest to me than the teapot!)
The D3DX library changed quite a bit from DirectX9 to DirectX11. Many of the helper functions were removed, including the shape drawing ones. However, DirectX11's DXUT library contains many of those functions you are looking for. In the DXUTOptional project, there is a DXUTShaped.h file, which contains DXUTCreateTeapot(). Here are all of the shape creation functions it supports...
HRESULT WINAPI DXUTCreateBox( ID3D10Device* pDevice, float fWidth, float fHeight, float fDepth, ID3DX10Mesh** ppMesh );
HRESULT WINAPI DXUTCreateCylinder( ID3D10Device* pDevice, float fRadius1, float fRadius2, float fLength, UINT uSlices, UINT uStacks, ID3DX10Mesh** ppMesh );
HRESULT WINAPI DXUTCreatePolygon( ID3D10Device* pDevice, float fLength, UINT uSides, ID3DX10Mesh** ppMesh );
HRESULT WINAPI DXUTCreateSphere( ID3D10Device* pDevice, float fRadius, UINT uSlices, UINT uStacks, ID3DX10Mesh** ppMesh );
HRESULT WINAPI DXUTCreateTorus( ID3D10Device* pDevice, float fInnerRadius, float fOuterRadius, UINT uSides, UINT uRings, ID3DX10Mesh** ppMesh );
HRESULT WINAPI DXUTCreateTeapot( ID3D10Device* pDevice, ID3DX10Mesh** ppMesh );
You can find the DXUT library where you installed the DirectX SDK. Mine is at
"C:\Program Files (x86)\Microsoft DirectX SDK (August 2009)\Samples\C++\DXUT11"
If you do not want to use DXUT in your project, you can just look at the source code in the DXUTOptional project and adapt it for you own purposes. All of DXUT's source code is available in the DXUTCore and DXUTOptional projects.
Good luck!

Resources