<div dir="ltr"><div>Let's jump to particulars, at the end, the code.<br><br></div><div>If I set the DATAPOINTER to NULL the program crashes at CreateCopy with access violation. Same if I set it to the datapointer to &abyRaster[0] and add the remaining two bands. <br><br></div><div>The abyRasters are initialized to white color with memset. SO this piece of code should give me a white jpg square of widthxheight. I could set a complete MWE if necessary.<br><br></div><div>And, the code:<br></div><div><br></div><span style="color:rgb(0,0,0)"><span style="font-family:monospace,monospace">        <span style="background-color:rgb(255,255,255)">       char* filename = (char*) malloc(1024);
</span></span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        sprintf(filename, "MEM:::DATAPOINTER=%p,PIXELS=%d,LINES=%d,BANDS=0",</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">                NULL, width, height);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        <span style="font-weight:600">poDstDS</span> = (GDALDataset *) GDALOpen(filename,GA_Update);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)"><br></span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        free(filename);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)"><br></span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        for(unsigned int b = 0; b < RGBRasterBands::<span style="font-weight:600">NUMBANDS</span>; b++)</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        {</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            //assert(sizeof(long) == sizeof(void*));</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(0,0,0)"></span><span style="color:rgb(0,0,0)"></span><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            const void * address = static_cast<const void*>(&<span style="font-weight:600">abyRasters</span>[b]);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            std::stringstream ss;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            ss << address;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            const char * datapointer = ss.str().c_str();</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            std::cout << "datapointer: " << datapointer << std::endl;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            papszOptions = CSLSetNameValue( papszOptions, "DATAPOINTER", datapointer );</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            <span style="font-weight:600">poDstDS</span>-><span style="font-style:italic">AddBand</span>(GDT_Byte, papszOptions);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)"><br></span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        }</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        char **papszOptions2 = NULL;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        papszOptions2 = CSLSetNameValue( papszOptions2, "QUALITY", "40" );</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)"><br></span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        std::cout << "Copying to " << filepath << std::endl;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        GDALDriver* jpegDriver = GetGDALDriverManager()->GetDriverByName("JPEG");</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        GDALDataset* jpeg_ds = jpegDriver->CreateCopy(filepath.string().c_str(), <span style="font-weight:600">poDstDS</span>, false, papszOptions2, GDALDummyProgress, NULL);</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        if(jpeg_ds == NULL)</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        {</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            std::cout << "Failure when creating output orthoimage " << filepath << std::endl;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">            return -5;</span></span></pre><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">
</span></span><pre style="margin-top:0px;margin-right:0px;margin-bottom:0px;text-indent:0px"><span style="background-color:rgb(255,255,255)"><span style="color:rgb(0,0,0)">        }</span><span style="font-family:arial,helvetica,sans-serif"><br><br>The <span style="font-family:monospace,monospace">abyRasters </span>are allocated with</span><span style="color:rgb(0,0,0)"><br><br>        for(int b = 0; b < RGBRasterBands::<span style="font-weight:600">NUMBANDS</span>; b++)
        {
            <span style="font-weight:600">abyRasters</span>.push_back(new GByte[<span style="font-weight:600">rasterMemoryDimensions</span>.<span style="font-weight:600">width</span>*<span style="font-weight:600">rasterMemoryDimensions</span>.<span style="font-weight:600">height</span>]());
            memset(&<span style="font-weight:600">abyRasters</span>[b][0],QuickDrop::<span style="font-weight:600">NODATAVALUE</span>,sizeof(GByte)*<span style="font-weight:600">rasterMemoryDimensions</span>.<span style="font-weight:600">width</span>*<span style="font-weight:600">rasterMemoryDimensions</span>.<span style="font-weight:600">height</span>);
        }</span></span><br></pre></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 6, 2016 at 12:35 PM, Pol Monsó Purtí <span dir="ltr"><<a href="mailto:lluna.nova@gmail.com" target="_blank">lluna.nova@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Just one more question,<br><br></div>If i set the option BAND=0, what should the DATAPOINTER be? Does it become optional?<br><br></div>Cheers<br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Mon, Jun 6, 2016 at 10:55 AM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@spatialys.com" target="_blank">even.rouault@spatialys.com</a>></span> wrote:<br></span><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Monday 06 June 2016 10:45:47 Pol Monsó Purtí wrote:<br>
> THanks Mateusz,<br>
><br>
> You're right. I'm trying it as we speak.<br>
><br>
> In my case I have a std::vector<GByte*> of the 3 bands, where the GByte*<br>
> are allocated in the heap with new GByte[width*height]. I guess that won't<br>
> work because the three bands have to be one single memory chunk, am I<br>
> right? I guess the only solution is to switch to single band float and<br>
> merge rgb to float. I'd prefer not having to refactor the rest of the code.<br>
<br>
<br>
> Is it possible to have a different address for each band?<br>
<br>
</span>Yes, create a dataset with 0 band and then add each band with the<br>
GDALDataset::AddBand() method<br>
<br>
See <a href="http://gdal.org/frmt_mem.html" rel="noreferrer" target="_blank">http://gdal.org/frmt_mem.html</a> :<br>
"""The MEM format is one of the few that supports the AddBand() method. The<br>
AddBand() method supports DATAPOINTER, PIXELOFFSET and LINEOFFSET options to<br>
reference an existing memory array."""<br>
<div><div><br>
><br>
> All this is to support jpg as output. All the operations are done in my<br>
> GByte array and it's only at the end that I create the dataset and image.<br>
> At the moment I create a Tiff and finally createcopy to jpg.<br>
><br>
> On Fri, Jun 3, 2016 at 9:10 PM, Mateusz Loskot <<a href="mailto:mateusz@loskot.net" target="_blank">mateusz@loskot.net</a>> wrote:<br>
> > On 3 June 2016 at 20:45, Pol Monsó Purtí <<a href="mailto:lluna.nova@gmail.com" target="_blank">lluna.nova@gmail.com</a>> wrote:<br>
> > > I've seen this mysterious article <a href="http://www.gdal.org/frmt_mem.html" rel="noreferrer" target="_blank">http://www.gdal.org/frmt_mem.html</a><br>
> ><br>
> > which<br>
> ><br>
> > > references the `DATAPOINTER` option. I've seen another reference<br>
> > > [here](<br>
> ><br>
> > <a href="https://lists.osgeo.org/pipermail/gdal-dev/2006-November/010583.html" rel="noreferrer" target="_blank">https://lists.osgeo.org/pipermail/gdal-dev/2006-November/010583.html</a>)<br>
> ><br>
> > > which does<br>
> ><br>
> > sprintf(filename,"MEM:::DATAPOINTER=%d,PIXELS=%d,LINES=%d,BANDS=1,DATATYPE<br>
> ><br>
=%d,PIXELOFFSET=0,LINEOFFSET=0,BANDOFFSET=0",datpt,pixels,lines,datatype);><br>
> > >     dataset = GDALOpen(filename,GA_Update);<br>
> > ><br>
> > > How would this translate to c++?<br>
> ><br>
> > The line above is valid C++, isn't it.<br>
> > If you followed to Frank's answer [1], you'd see it slightly corrected<br>
> > but the technique remains the same.<br>
> ><br>
> > [1] <a href="https://lists.osgeo.org/pipermail/gdal-dev/2006-November/010603.html" rel="noreferrer" target="_blank">https://lists.osgeo.org/pipermail/gdal-dev/2006-November/010603.html</a><br>
> ><br>
> > Best regards,<br>
> > --<br>
> > Mateusz Loskot, <a href="http://mateusz.loskot.net" rel="noreferrer" target="_blank">http://mateusz.loskot.net</a><br>
<br>
</div></div><span><font color="#888888">--<br>
Spatialys - Geospatial professional services<br>
<a href="http://www.spatialys.com" rel="noreferrer" target="_blank">http://www.spatialys.com</a><br>
</font></span></blockquote></div></div></div><br></div>
</blockquote></div><br></div>