Results 1 to 3 of 3

Thread: External Lib read from QBuffer on Calback problem

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default External Lib read from QBuffer on Calback problem

    I read compressed image from libpng .. my EncoderWriteCallback write is ok and running
    but the read (EncoderReadCallback) is not ok why?

    is the same 1:1 QByteArray wo libpng has writteln ... why now it can not read?
    the QByteArray is not from qpixmap or qimage.

    on debug qbuffer is readable and having size ..


    I start the callback so...
    Qt Code:
    1. png_ptr_read = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    2. if(png_ptr_read == NULL) {
    3. qWarning("unable to create read struct");
    4. return;
    5. }
    6. info_ptr_read = png_create_info_struct(png_ptr_read);
    7. if(info_ptr_read == NULL) {
    8. qWarning("unable to create info struct");
    9. return;
    10. }
    11. png_set_sig_bytes(png_ptr_read, 8);
    12. APNGImageStream myqtchunk(0,frame.stream()); /* stream() = QByteArray */
    13. png_set_read_fn(png_ptr_read,&myqtchunk,EncoderReadCallback);
    To copy to clipboard, switch view to plain text mode 

    and this is reader and writer
    Qt Code:
    1. #define PNG_USER_MEM_SUPPORTED
    2. #define PNG_WRITE_bKGD_SUPPORTED
    3.  
    4.  
    5. ////base ref http://www.littlesvr.ca/apng/tutorial/x148.html
    6. /* png lib from firefox 3 is patch to APNG format! */
    7. #include "../moz_png/png.h"
    8.  
    9. /* device to write or read image frames */
    10. class APNGImageStream {
    11. public:
    12. APNGImageStream( const int mode , QByteArray in = QByteArray() )
    13. :dev(new QBuffer()),bytes(in)
    14. {
    15. dev->setBuffer(&bytes);
    16. if (mode == 1) {
    17. dev->open(QIODevice::WriteOnly);
    18. } else {
    19. dev->open(QIODevice::ReadOnly);
    20. }
    21. }
    22. ~APNGImageStream()
    23. {
    24. dev->close();
    25. }
    26. QBuffer *device() { return dev; }
    27. bool isValid() { return img.loadFromData(dev->data()); }
    28. QByteArray stream() { return dev->data(); }
    29. QImage img;
    30. QBuffer *dev;
    31. QByteArray bytes;
    32. };
    33.  
    34. // Called by libpng to flush its internal buffer to ours.
    35. static void EncoderWriteCallback(png_structp png, png_bytep data,
    36. png_size_t size) {
    37. APNGImageStream *state = static_cast<APNGImageStream*>(png_get_io_ptr(png));
    38. Q_ASSERT(state->device());
    39. state->device()->write((char*)data,size);
    40. }
    41.  
    42. // Called by libpng to read original QByteArray that has self created .
    43. static void EncoderReadCallback(png_structp png, png_bytep data,
    44. png_size_t sizer) {
    45. APNGImageStream *state = static_cast<APNGImageStream*>(png_get_io_ptr(png));
    46. Q_ASSERT(state->device());
    47. qDebug() << "### size to read 1/0 " << state->stream().size() << "," << state->device()->isReadable();
    48. state->device()->read((char*)data,sizer);
    49. qDebug() << "### read callback length " << sizer;
    50. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: External Lib read from QBuffer on Calback problem

    i try to write on device and i leave dev->setBuffer(&bytes);
    is the same, libpng can only write on device .. why not read ..

    qt access the image isValid on read & write ....

    i can not set file over 100 png frame movie....


    Qt Code:
    1. public:
    2. APNGImageStream( const int mode , QByteArray in = QByteArray() )
    3. :dev(new QBuffer()),bytes(in)
    4. {
    5. if (mode == 1) {
    6. /* pnglib write in */
    7. dev->setBuffer(&bytes);
    8. dev->open(QIODevice::WriteOnly);
    9. } else {
    10. /* qt write in && png read out */
    11. dev->open(QIODevice::ReadWrite);
    12. dev->write(bytes);
    13. dev->seek(0);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Solved:: External Lib read from QBuffer on Calback problem

    I have resolved the big QBuffer problem
    only writeBlock && readBlock and all external lib ... in this case libpng can write or read like a virtual file... && class PngEventBuffer : public QEvent the post event can paint a progress dialog by read or write....

    QEvent inside a CallBack function .. this is new tecnology...


    Qt Code:
    1. class APNGwrittelStream {
    2. public:
    3. APNGwrittelStream(QObject *o) :d(new QBuffer()),creator(o)
    4. {
    5. d->open(QIODevice::ReadWrite);
    6. start();
    7. }
    8. ~APNGwrittelStream()
    9. {
    10. d->close();
    11. }
    12. void start() { d->seek(0); }
    13. QBuffer *device() { return d; }
    14. bool isValid() { return img.loadFromData(stream()); }
    15. QByteArray stream() { return d->data(); }
    16. QImage img;
    17. QBuffer *d;
    18. QObject *creator;
    19. };
    20.  
    21.  
    22. /* read and write progress */
    23. class PngEventBuffer : public QEvent
    24. {
    25. //QEvent::User==1000
    26.  
    27. public:
    28. PngEventBuffer(int status, int total )
    29. : QEvent( QEvent::User ),s(status),t(total)
    30. { }
    31. int pos() const { return s; }
    32. int sum() const { return t; }
    33.  
    34. private:
    35. int s;
    36. int t;
    37. };
    38.  
    39.  
    40. // Called by libpng to flush its internal buffer to ours.
    41. static void EncoderWriteCallback(png_structp png, png_bytep data,
    42. png_size_t size) {
    43. APNGwrittelStream *state = static_cast<APNGwrittelStream*>(png_get_io_ptr(png));
    44. Q_ASSERT(state->device());
    45. state->device()->writeBlock((char*)data,size);
    46. ///////qDebug() << "### pos ->" << state->device()->pos();
    47. qApp->postEvent(state->creator,new PngEventBuffer(state->device()->pos(),
    48. 0));
    49.  
    50. }
    51.  
    52. // Called by libpng to read QImage on buffer .
    53. static void EncoderReaderCallback(png_structp png, png_bytep data,
    54. png_size_t size)
    55. {
    56. APNGwrittelStream *state = static_cast<APNGwrittelStream*>(png_get_io_ptr(png));
    57. Q_ASSERT(state->device());
    58. state->device()->readBlock((char*)data,size);
    59. /////////qDebug() << "### pos ->" << state->device()->pos();
    60. qApp->postEvent(state->creator,new PngEventBuffer(state->device()->pos(),
    61. state->device()->bytesAvailable()));
    62. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by patrik08; 2nd June 2008 at 19:45. Reason: reformatted to look better

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.