<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic MIMXRT1060-EVK encoding JPEG to microSD card in i.MX RT Crossover MCUs</title>
    <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/MIMXRT1060-EVK-encoding-JPEG-to-microSD-card/m-p/1155843#M10173</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to make a program where a camera image is saved onto a microSD card plugged into the EVK.&amp;nbsp; I've been using the csi_565 and sd_jpeg demos and the "Making a Camera Application" demo for the RT1050 as reference, and the program &lt;STRIKE&gt;is running into insufficient memory issues when it tries to start the JPEG compressor.&amp;nbsp; I've tried reducing the image size from 720p to 240p and the OUTPUT_BUF_SIZE in libjpeg's jdatadst.c, but that only postpones the program from running out of memory to allocate to later in the compressor startup.&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;EDIT: Right after I posted this I realized that heap and stack were set to default sizes.&amp;nbsp; Manually set their sizes and fixed the memory issues, but now I'm getting " Improper call to JPEG library in state 0" errors.&lt;/P&gt;&lt;P&gt;EDIT 2: Improper library calls resolved by updating FREAD and FWRITE in jconfig.h to the read_file and write_file in jconfig.h in sd_jpeg demo.&lt;/P&gt;&lt;P&gt;Here's the main function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;int main(void)
{
	int j = 0;
	uint32_t csiFrameBufPtr1;

    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_InitDEBUG_UARTPins();
    BOARD_InitSDRAMPins();
    BOARD_EarlyPrepareCamera();
    BOARD_InitCSIPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("CSI RGB565 example start...\r\n");

    APP_InitCamera(); //from csi_rgb565

    CAMERA_RECEIVER_Start(&amp;amp;cameraReceiver);
    if (0 != MOUNT_SDCard()) //Mount_SDCard() from sd_jpeg
	{
		PRINTF("SD card mount error. Demo stopped!");
		return -1;
	}
	while (kStatus_Success != CAMERA_RECEIVER_GetFullBuffer(&amp;amp;cameraReceiver, &amp;amp;csiFrameBufPtr1))
	{
	}
	CAMERA_RECEIVER_Stop(&amp;amp;cameraReceiver);
	JpegCompress(csiFrameBufPtr1);
	return 0;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;and here's the JpegCompress function, which is from the "Making A Camera App." demo:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;static status_t JpegCompress(uint32_t buffer){
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	uint16_t (*scanlines)[IMAGE_WIDTH] = (uint16_t (*)[IMAGE_WIDTH])buffer;
	JSAMPROW row_pointer;

	FILE fJpeg;
	char JpegFilename[20] = "/IMG_000.jpg";
	static uint32_t JpegFilenameIndex = 0u;

	PRINTF("Start of JPEG Compression... ");

	if(JpegFilenameIndex &amp;gt; 9999u)
	{
		JpegFilenameIndex = 0u;
	}

	//sprintf(JpegFilename, "/IMG_%04d.jpg", ++JpegFilenameIndex);

	/* Open the output file. */
	if(f_open(&amp;amp;fJpeg, _T(JpegFilename), (FA_WRITE | FA_CREATE_ALWAYS)))
	{
		PRINTF("ERROR: can't open %s\n", JpegFilename);
		return kStatus_Fail;
	}

	/* Initialize the JPEG compression object with default error handling. */
	cinfo.err = jpeg_std_error(&amp;amp;jerr);
	jpeg_create_compress(&amp;amp;cinfo);

	/* Initialize JPEG parameters.*/
	cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
	jpeg_set_defaults(&amp;amp;cinfo);

	/* Specify the source format. */
	cinfo.in_color_space = JCS_GRAYSCALE;
	cinfo.input_components = 1u;
	cinfo.data_precision = 8u;
	cinfo.image_width = (JDIMENSION)IMAGE_WIDTH;
	cinfo.image_height = (JDIMENSION)IMAGE_HEIGHT;

	/* Now that we know input colorspace, fix colorspace-dependent defaults */
	jpeg_default_colorspace(&amp;amp;cinfo);

	/* Specify data destination for compression */
	jpeg_stdio_dest(&amp;amp;cinfo, &amp;amp;fJpeg);

	jpeg_set_quality(&amp;amp;cinfo, 50u, true);

	/* Start compressor */
	jpeg_start_compress(&amp;amp;cinfo, TRUE);

	/* Process data */
	while (cinfo.next_scanline &amp;lt; cinfo.image_height)
	{
		rgb565_2_rgb888((uint8_t *)(scanlines + cinfo.next_scanline), (uint8_t *)(JpegScanlines), IMAGE_WIDTH);
		row_pointer = (JSAMPROW)JpegScanlines;
		jpeg_write_scanlines(&amp;amp;cinfo, &amp;amp;row_pointer, COMPRESS_LINES);
	}

	/* Finish compression and release memory */
	jpeg_finish_compress(&amp;amp;cinfo);
	jpeg_destroy_compress(&amp;amp;cinfo);

	f_close(&amp;amp;fJpeg);

	PRINTF("Done: %s is saved. \r\n", JpegFilename);

	return kStatus_Success;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;&amp;nbsp;My hypothesis is that there's a project setting that's limiting memory access, although I'm not sure where I would find those settings.&lt;/STRIKE&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Sep 2020 15:36:32 GMT</pubDate>
    <dc:creator>pagecache</dc:creator>
    <dc:date>2020-09-20T15:36:32Z</dc:date>
    <item>
      <title>MIMXRT1060-EVK encoding JPEG to microSD card</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/MIMXRT1060-EVK-encoding-JPEG-to-microSD-card/m-p/1155843#M10173</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to make a program where a camera image is saved onto a microSD card plugged into the EVK.&amp;nbsp; I've been using the csi_565 and sd_jpeg demos and the "Making a Camera Application" demo for the RT1050 as reference, and the program &lt;STRIKE&gt;is running into insufficient memory issues when it tries to start the JPEG compressor.&amp;nbsp; I've tried reducing the image size from 720p to 240p and the OUTPUT_BUF_SIZE in libjpeg's jdatadst.c, but that only postpones the program from running out of memory to allocate to later in the compressor startup.&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;EDIT: Right after I posted this I realized that heap and stack were set to default sizes.&amp;nbsp; Manually set their sizes and fixed the memory issues, but now I'm getting " Improper call to JPEG library in state 0" errors.&lt;/P&gt;&lt;P&gt;EDIT 2: Improper library calls resolved by updating FREAD and FWRITE in jconfig.h to the read_file and write_file in jconfig.h in sd_jpeg demo.&lt;/P&gt;&lt;P&gt;Here's the main function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;int main(void)
{
	int j = 0;
	uint32_t csiFrameBufPtr1;

    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_InitDEBUG_UARTPins();
    BOARD_InitSDRAMPins();
    BOARD_EarlyPrepareCamera();
    BOARD_InitCSIPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("CSI RGB565 example start...\r\n");

    APP_InitCamera(); //from csi_rgb565

    CAMERA_RECEIVER_Start(&amp;amp;cameraReceiver);
    if (0 != MOUNT_SDCard()) //Mount_SDCard() from sd_jpeg
	{
		PRINTF("SD card mount error. Demo stopped!");
		return -1;
	}
	while (kStatus_Success != CAMERA_RECEIVER_GetFullBuffer(&amp;amp;cameraReceiver, &amp;amp;csiFrameBufPtr1))
	{
	}
	CAMERA_RECEIVER_Stop(&amp;amp;cameraReceiver);
	JpegCompress(csiFrameBufPtr1);
	return 0;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;and here's the JpegCompress function, which is from the "Making A Camera App." demo:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;static status_t JpegCompress(uint32_t buffer){
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	uint16_t (*scanlines)[IMAGE_WIDTH] = (uint16_t (*)[IMAGE_WIDTH])buffer;
	JSAMPROW row_pointer;

	FILE fJpeg;
	char JpegFilename[20] = "/IMG_000.jpg";
	static uint32_t JpegFilenameIndex = 0u;

	PRINTF("Start of JPEG Compression... ");

	if(JpegFilenameIndex &amp;gt; 9999u)
	{
		JpegFilenameIndex = 0u;
	}

	//sprintf(JpegFilename, "/IMG_%04d.jpg", ++JpegFilenameIndex);

	/* Open the output file. */
	if(f_open(&amp;amp;fJpeg, _T(JpegFilename), (FA_WRITE | FA_CREATE_ALWAYS)))
	{
		PRINTF("ERROR: can't open %s\n", JpegFilename);
		return kStatus_Fail;
	}

	/* Initialize the JPEG compression object with default error handling. */
	cinfo.err = jpeg_std_error(&amp;amp;jerr);
	jpeg_create_compress(&amp;amp;cinfo);

	/* Initialize JPEG parameters.*/
	cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
	jpeg_set_defaults(&amp;amp;cinfo);

	/* Specify the source format. */
	cinfo.in_color_space = JCS_GRAYSCALE;
	cinfo.input_components = 1u;
	cinfo.data_precision = 8u;
	cinfo.image_width = (JDIMENSION)IMAGE_WIDTH;
	cinfo.image_height = (JDIMENSION)IMAGE_HEIGHT;

	/* Now that we know input colorspace, fix colorspace-dependent defaults */
	jpeg_default_colorspace(&amp;amp;cinfo);

	/* Specify data destination for compression */
	jpeg_stdio_dest(&amp;amp;cinfo, &amp;amp;fJpeg);

	jpeg_set_quality(&amp;amp;cinfo, 50u, true);

	/* Start compressor */
	jpeg_start_compress(&amp;amp;cinfo, TRUE);

	/* Process data */
	while (cinfo.next_scanline &amp;lt; cinfo.image_height)
	{
		rgb565_2_rgb888((uint8_t *)(scanlines + cinfo.next_scanline), (uint8_t *)(JpegScanlines), IMAGE_WIDTH);
		row_pointer = (JSAMPROW)JpegScanlines;
		jpeg_write_scanlines(&amp;amp;cinfo, &amp;amp;row_pointer, COMPRESS_LINES);
	}

	/* Finish compression and release memory */
	jpeg_finish_compress(&amp;amp;cinfo);
	jpeg_destroy_compress(&amp;amp;cinfo);

	f_close(&amp;amp;fJpeg);

	PRINTF("Done: %s is saved. \r\n", JpegFilename);

	return kStatus_Success;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;&amp;nbsp;My hypothesis is that there's a project setting that's limiting memory access, although I'm not sure where I would find those settings.&lt;/STRIKE&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Sep 2020 15:36:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/MIMXRT1060-EVK-encoding-JPEG-to-microSD-card/m-p/1155843#M10173</guid>
      <dc:creator>pagecache</dc:creator>
      <dc:date>2020-09-20T15:36:32Z</dc:date>
    </item>
  </channel>
</rss>

