Memory allocation of 3-d arrays for reading pixel data of images using
libjpeg
I am trying to copy the pixel data of an image to a matrix using libjpeg.
Something is going wrong with the pointers. Please help.
The problem in brief: I pass an int ***p pointer to a function which reads
pixel data. Within the function, I am able to access the elements
p[i][j][k] and perform operations on them but when I try to do the same in
main the program crashes. The main function is:
#include<stdio.h>
#include<stdlib.h>
#include<jpeglib.h>
#include"functions.h"
void main(void)
{
char * file="a.jpg";
int ***p; // to store the pixel values
int s[2]={0,0}; // to store the dimensions of the image
read_JPEG_file(file,p,s); // Function that reads the image
printf("%d",p[0][0][0]); // This makes the program crash
}
The file functions.h reads:
int read_JPEG_file(char * file, int ***p, int **s)
{
int i,j;
//-----------------libjpeg procedure which I got from the
documentation------------
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err=jpeg_std_error(&jerr);
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride;
if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE,
row_stride, 1);
s[0]=cinfo.output_height;
s[1]=cinfo.output_width;
p=(int ***)malloc(s[0]*sizeof(int **));
for (i=0;i<s[0];i++)
{
p[i]=(int **)malloc(s[1]*sizeof(int *));
for (j=0;j<s[1];j++)
{
p[i][j]=(int *)malloc(3*sizeof(int));
}
}
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
for ( i=0; i<cinfo.output_width; i++)
{
p[cinfo.output_scanline-1][i][0]=(int)buffer[0][0+3*i];
p[cinfo.output_scanline-1][i][1]=(int)buffer[0][1+3*i];
p[cinfo.output_scanline-1][i][2]=(int)buffer[0][2+3*i];
}
}
printf("%d",p[0][0][0]); // This works just fine
return 0;
}
I know that something is wrong with the memory allocation but I don't know
what. I tried another test program and successfully allocated a memory
block 500X500X500 integers long, and it was working-it was outputting
random integers without crashing- so lack of memory is not a problem.
Borden
Thursday, 3 October 2013
Wednesday, 2 October 2013
Implications of $\|Ax\|_1\leq \|Bx\|_1$
Implications of $\|Ax\|_1\leq \|Bx\|_1$
Let us assume that $A,B\in\mathbb{R}^{m\times n}$ and $\|\cdot\|_1$ is the
(vector) norm-1 defined as $\|x\|_1=\sum_{i=1}^n|x(i)|$, and the induced
matrix norm is $\|A\|_1=\sup_{\|x\|_1=1}\|Ax\|_1$. Let us assume that:
$$ \|Ax\|_1 \leq \|Bx\|_1, \forall x\in\mathbb{R}^n. $$
What does this imply about $A$ and $B$? I can only tell that if $n=m$ and
$B$ is invertible then we may set $y=Bx$ so $x=B^{-1}y$, hence the above
inequality becomes:
$$ \|AB^{-1}y\|_1 \leq \|y\|_1, \forall x\in\mathbb{R}^n, $$
Therefore,
$$ \|AB^{-1}\|_{1}\leq 1 $$
But, I would be interested in the case where $B$ is not assumed to be
invertible and may also not be a square matrix.
Let us assume that $A,B\in\mathbb{R}^{m\times n}$ and $\|\cdot\|_1$ is the
(vector) norm-1 defined as $\|x\|_1=\sum_{i=1}^n|x(i)|$, and the induced
matrix norm is $\|A\|_1=\sup_{\|x\|_1=1}\|Ax\|_1$. Let us assume that:
$$ \|Ax\|_1 \leq \|Bx\|_1, \forall x\in\mathbb{R}^n. $$
What does this imply about $A$ and $B$? I can only tell that if $n=m$ and
$B$ is invertible then we may set $y=Bx$ so $x=B^{-1}y$, hence the above
inequality becomes:
$$ \|AB^{-1}y\|_1 \leq \|y\|_1, \forall x\in\mathbb{R}^n, $$
Therefore,
$$ \|AB^{-1}\|_{1}\leq 1 $$
But, I would be interested in the case where $B$ is not assumed to be
invertible and may also not be a square matrix.
How to properly use the IF Statement
How to properly use the IF Statement
Which one is correct and WHY
in both examples we have a function that determines if a certain string is
valid...
(using some other function that's not defined here)
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (Validator.Validate(foo))
{
return true;
}
else
{
return false;
}
}
in the second scenario we have a function that ends with a TRUE statement
and with no else.
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (!Validator.Validate(foo))
{
return false;
}
return true;
}
NOW INB4 please dont say that you can simply do it this way
return Validator.Validate(foo);
How to save a few lines its not what i want to know...but the implications
and unknown consecuences ( to me ) of using one method or the other.
Which one is correct and WHY
in both examples we have a function that determines if a certain string is
valid...
(using some other function that's not defined here)
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (Validator.Validate(foo))
{
return true;
}
else
{
return false;
}
}
in the second scenario we have a function that ends with a TRUE statement
and with no else.
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (!Validator.Validate(foo))
{
return false;
}
return true;
}
NOW INB4 please dont say that you can simply do it this way
return Validator.Validate(foo);
How to save a few lines its not what i want to know...but the implications
and unknown consecuences ( to me ) of using one method or the other.
skip elements in each function by conditions
skip elements in each function by conditions
how can I skip some elements in each function of jquery by the next
condition:
var number_of_td = 0;
$('td').each(function() {
if (number_of_td == 0) {
if ($(this).attr('id') == "1") {
//skip the next three elements:
//something like: $(this) = $(this).next().next().next();
}
}
else if (number_of_td == 1) {
if ($(this).attr('id') == "2") {
//skip the next two elements
}
}
else if (number_of_td == 2) {
if ($(this).attr('id') == "3") {
//skip the next element
}
}
else if (number_of_td == 3) {
if ($(this).attr('id') == "4") {
//skip the next element
}
}
else {
number_of_td++;
if (number_of_td == 4) {
number_of_td = 0;
}
}
});
for example:
<td attr="1"></td>
<td attr="6"></td>
<td attr="7"></td>
<td attr="9"></td>
//-------------------
<td attr="2"></td>
<td attr="5"></td>
<td attr="3"></td>
<td attr="6"></td>
//-------------------
<td attr="7"></td>
<td attr="2"></td>
<td attr="8"></td>
<td attr="6"></td>
if one of the 4th conditions exists, skip till the td element with attr=2.
in this example, the first td attribute is 1, so it skips till attr=2 and
not check the other elements (attr=6,7,9).
2 is not equal to 1, 5 is not equal to 2, 3 is equal to 3, so it skips
till attr=7, etc.
I hope you can understand my example.
any help appreciated!
how can I skip some elements in each function of jquery by the next
condition:
var number_of_td = 0;
$('td').each(function() {
if (number_of_td == 0) {
if ($(this).attr('id') == "1") {
//skip the next three elements:
//something like: $(this) = $(this).next().next().next();
}
}
else if (number_of_td == 1) {
if ($(this).attr('id') == "2") {
//skip the next two elements
}
}
else if (number_of_td == 2) {
if ($(this).attr('id') == "3") {
//skip the next element
}
}
else if (number_of_td == 3) {
if ($(this).attr('id') == "4") {
//skip the next element
}
}
else {
number_of_td++;
if (number_of_td == 4) {
number_of_td = 0;
}
}
});
for example:
<td attr="1"></td>
<td attr="6"></td>
<td attr="7"></td>
<td attr="9"></td>
//-------------------
<td attr="2"></td>
<td attr="5"></td>
<td attr="3"></td>
<td attr="6"></td>
//-------------------
<td attr="7"></td>
<td attr="2"></td>
<td attr="8"></td>
<td attr="6"></td>
if one of the 4th conditions exists, skip till the td element with attr=2.
in this example, the first td attribute is 1, so it skips till attr=2 and
not check the other elements (attr=6,7,9).
2 is not equal to 1, 5 is not equal to 2, 3 is equal to 3, so it skips
till attr=7, etc.
I hope you can understand my example.
any help appreciated!
Is there an open source engine to provide related topics?
Is there an open source engine to provide related topics?
I am looking for an open source API or library that, given a query, will
generate related topics
For example:
Programming might be linked to c++, code and Java
Car might be linked to truck, Ford and Holden
Social science might be linked to history, philosophy, psychology
I am looking for an open source API or library that, given a query, will
generate related topics
For example:
Programming might be linked to c++, code and Java
Car might be linked to truck, Ford and Holden
Social science might be linked to history, philosophy, psychology
Tuesday, 1 October 2013
Doctrine2 Self Reference Query -- Doesn't work
Doctrine2 Self Reference Query -- Doesn't work
$ I am trying to create following scenario with doctrine 2 query builder
SELECT
p . *
FROM
_tree p
LEFT JOIN
_tree c ON p.id = c.parent_id
AND (c.lft >= p.lft AND c.rgt <= p.rgt)
WHERE
p.id = 3
I have set following relationship self generated by Doctrine2
class Tree {
/**
* @var \Tree
*
* @ORM\ManyToOne(targetEntity="Tree")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* })
*/
private $parent;
// other code
}
here is my repo class
_em->createQueryBuilder();
$qb->select('p')
->from('Entity\Tree', 'p')
->leftJoin('p.Entity\Tree','c', 'ON','p.id = c.parent_id');
return $qb->getQuery()->getResult();
}
}
but I couldn't get it done. It throws following errors
[Tue Oct 01 22:30:11 2013] [error] [client 127.0.0.1] PHP Fatal error:
Uncaught exception 'Doctrine\ORM\Query\QueryException' with message
'SELECT p FROM Entity\Tree p LEFT JOIN p.Entity\Tree c ON p.id =
c.parent_id' in
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php:39\nStack
trace:\n#0
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(429):
Doctrine\ORM\Query\QueryException::dqlError('SELECT p FROM E...')\n#1
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(925):
Doctrine\ORM\Query\Parser->semanticalError('Class Entity\Ed...')\n#2
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1561):
Doctrine\ORM\Query\Parser->JoinAssociationPathExpression()\n#3
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1506):
Doctrine\ORM\Query\Parser->JoinAssociationDeclaration()\n#4
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1435):
Doctrine\ORM\Query\Parser->Join()\n#5
/var/www/pcb_frame_work/System/Librari in
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php
on line 49, referer:
$ I am trying to create following scenario with doctrine 2 query builder
SELECT
p . *
FROM
_tree p
LEFT JOIN
_tree c ON p.id = c.parent_id
AND (c.lft >= p.lft AND c.rgt <= p.rgt)
WHERE
p.id = 3
I have set following relationship self generated by Doctrine2
class Tree {
/**
* @var \Tree
*
* @ORM\ManyToOne(targetEntity="Tree")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* })
*/
private $parent;
// other code
}
here is my repo class
_em->createQueryBuilder();
$qb->select('p')
->from('Entity\Tree', 'p')
->leftJoin('p.Entity\Tree','c', 'ON','p.id = c.parent_id');
return $qb->getQuery()->getResult();
}
}
but I couldn't get it done. It throws following errors
[Tue Oct 01 22:30:11 2013] [error] [client 127.0.0.1] PHP Fatal error:
Uncaught exception 'Doctrine\ORM\Query\QueryException' with message
'SELECT p FROM Entity\Tree p LEFT JOIN p.Entity\Tree c ON p.id =
c.parent_id' in
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php:39\nStack
trace:\n#0
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(429):
Doctrine\ORM\Query\QueryException::dqlError('SELECT p FROM E...')\n#1
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(925):
Doctrine\ORM\Query\Parser->semanticalError('Class Entity\Ed...')\n#2
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1561):
Doctrine\ORM\Query\Parser->JoinAssociationPathExpression()\n#3
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1506):
Doctrine\ORM\Query\Parser->JoinAssociationDeclaration()\n#4
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1435):
Doctrine\ORM\Query\Parser->Join()\n#5
/var/www/pcb_frame_work/System/Librari in
/var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php
on line 49, referer:
Cannot set focus to a form field in a jQuery UI dialog on clicking a jQueryUI menu item
Cannot set focus to a form field in a jQuery UI dialog on clicking a
jQueryUI menu item
I've got a jQuery UI dialog containing a one-field form and the autoOpen
property is set to false at the beginning. There's another jQuery UI menu
on the page and the dialog's open function is binding to the click event
of the menu items. I've been trying to set focus to the only form field of
the dialog when the dialog is open on click of the menu items somehow no
luck. To pinpoint the cause I also added another test button and by
clicking that button I can set focus to the form field. So I'm pretty sure
it's the jQuery UI menu that is preventing the focus of the field. I
wonder if there's any way that I can circumvent this constraint. Any
insight is appreciated. Thanks!
<ul id="menu">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
</ul>
</br>
<button id="btn">Open the dialog</button>
<div id="dialog" title="Basic dialog">
<form>
<input type="text" id="fld" />
</form>
</div>
$( "#dialog" ).dialog({
autoOpen: false,
open: function(event, ui){
$('#fld').focus();
}
});
$('#btn').click(function(e){
$( "#dialog" ).dialog('open');
});
$('#menu li a').click(function(){
$( "#dialog" ).dialog('open');
})
$( "#menu" ).menu({
select: function( event, ui ) {
$( "#dialog" ).dialog('open');
}
});
Here is the js fiddle
jQueryUI menu item
I've got a jQuery UI dialog containing a one-field form and the autoOpen
property is set to false at the beginning. There's another jQuery UI menu
on the page and the dialog's open function is binding to the click event
of the menu items. I've been trying to set focus to the only form field of
the dialog when the dialog is open on click of the menu items somehow no
luck. To pinpoint the cause I also added another test button and by
clicking that button I can set focus to the form field. So I'm pretty sure
it's the jQuery UI menu that is preventing the focus of the field. I
wonder if there's any way that I can circumvent this constraint. Any
insight is appreciated. Thanks!
<ul id="menu">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
</ul>
</br>
<button id="btn">Open the dialog</button>
<div id="dialog" title="Basic dialog">
<form>
<input type="text" id="fld" />
</form>
</div>
$( "#dialog" ).dialog({
autoOpen: false,
open: function(event, ui){
$('#fld').focus();
}
});
$('#btn').click(function(e){
$( "#dialog" ).dialog('open');
});
$('#menu li a').click(function(){
$( "#dialog" ).dialog('open');
})
$( "#menu" ).menu({
select: function( event, ui ) {
$( "#dialog" ).dialog('open');
}
});
Here is the js fiddle
Subscribe to:
Comments (Atom)