Make a monocolor jpeg file encoded one bit per pixel? - imagemagick

For example, I see the following output. I don't quite understand its complete meaning. But when I look at the figure, it is just black and white, without gray or other color.
$ file 0447.jpg
0447.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1725x2751, components 1
The file size is 652708. To encode, 1725x2751 bits, the file size should be no more than 1725 * 2751/8 = 593184.4. But the file size is bigger than this.
So it seems that a pixel is not encoded as one bit. Is there a way to shrink the size of the file (without loss of info) so that one pixel is one bit (maybe using other formats)?

Don't use IM identify to find the actual bits-per pixel. Use EXIFTOOL.
Input JPG:
Exiftool shows BitsPerSample of 8
exiftool -s -ee -g1 -u -n -D cow.jpg
---- ExifTool ----
- ExifToolVersion : 12.44
---- System ----
- FileName : cow.jpg
- Directory : .
- FileSize : 16214
- FileModifyDate : 2022:10:14 11:31:44-07:00
- FileAccessDate : 2022:10:14 11:31:44-07:00
- FileInodeChangeDate : 2022:10:14 11:31:44-07:00
- FilePermissions : 100644
---- File ----
- FileType : JPEG
- FileTypeExtension : JPG
- MIMEType : image/jpeg
- ImageWidth : 300
- ImageHeight : 300
- EncodingProcess : 0
- BitsPerSample : 8 <-------
- ColorComponents : 1
---- JFIF ----
0 JFIFVersion : 1 1
2 ResolutionUnit : 2
3 XResolution : 28
5 YResolution : 28
---- Composite ----
- ImageSize : 300 300
- Megapixels : 0.09
Threshold to GIF:
convert cow.jpg -threshold 50% cow_bw1.gif
Exiftool shows BitsPerPixel = 1
exiftool -s -ee -g1 -u -n -D cow_bw1.gif
---- ExifTool ----
- ExifToolVersion : 12.44
---- System ----
- FileName : cow_bw1.gif
- Directory : .
- FileSize : 2698
- FileModifyDate : 2022:10:14 11:44:17-07:00
- FileAccessDate : 2022:10:14 11:44:19-07:00
- FileInodeChangeDate : 2022:10:14 11:44:17-07:00
- FilePermissions : 100644
---- File ----
- FileType : GIF
- FileTypeExtension : GIF
- MIMEType : image/gif
---- GIF ----
- GIFVersion : 89a
0 ImageWidth : 300
2 ImageHeight : 300
4.1 HasColorMap : 1
4.2 ColorResolutionDepth : 8
4.3 BitsPerPixel : 1 <-------
5 BackgroundColor : 1
---- Composite ----
- ImageSize : 300 300
- Megapixels : 0.09
Threshold to PNG:
convert cow.jpg -threshold 50% cow_bw1.png
Exiftool shows BitDepth of 1
exiftool -s -ee -g1 -u -n -D cow_bw1.png
---- ExifTool ----
- ExifToolVersion : 12.44
- Warning : [minor] Text/EXIF chunk(s) found after PNG IDAT (may be ignored by some readers)
---- System ----
- FileName : cow_bw1.png
- Directory : .
- FileSize : 2251
- FileModifyDate : 2022:10:14 11:49:20-07:00
- FileAccessDate : 2022:10:14 11:49:21-07:00
- FileInodeChangeDate : 2022:10:14 11:49:20-07:00
- FilePermissions : 100644
---- File ----
- FileType : PNG
- FileTypeExtension : PNG
- MIMEType : image/png
---- PNG ----
0 ImageWidth : 300
4 ImageHeight : 300
8 BitDepth : 1 <-------
9 ColorType : 0
10 Compression : 0
11 Filter : 0
12 Interlace : 0
- Gamma : 2.2
- BackgroundColor : 1
- ModifyDate : 2022:10:14 18:49:20
- Datecreate : 2022-10-14T18:31:44+00:00
- Datemodify : 2022-10-14T18:31:44+00:00
- Datetimestamp : 2022-10-14T18:49:19+00:00
---- PNG-pHYs ----
0 PixelsPerUnitX : 2800
4 PixelsPerUnitY : 2800
8 PixelUnits : 1
---- Composite ----
- ImageSize : 300 300
- Megapixels : 0.09
ADDITION
Here is how to make a 2-bit TIFF file.
convert lena.jpg -colorspace gray -depth 2 -type palette lena_d2.tif
exiftool -s -ee -g1 -u -n -D lena_d2.tif
---- ExifTool ----
- ExifToolVersion : 12.44
---- System ----
- FileName : lena_d2.tif
- Directory : .
- FileSize : 16642
- FileModifyDate : 2022:10:14 16:42:42-07:00
- FileAccessDate : 2022:10:14 16:42:43-07:00
- FileInodeChangeDate : 2022:10:14 16:42:42-07:00
- FilePermissions : 100644
---- File ----
- FileType : TIFF
- FileTypeExtension : TIF
- MIMEType : image/tiff
- ExifByteOrder : II
---- IFD0 ----
256 ImageWidth : 256
257 ImageHeight : 256
258 BitsPerSample : 2 <------
259 Compression : 1
262 PhotometricInterpretation : 3
266 FillOrder : 1
273 StripOffsets : 8
274 Orientation : 1
277 SamplesPerPixel : 1
278 RowsPerStrip : 256
279 StripByteCounts : 16384
282 XResolution : 72
283 YResolution : 72
284 PlanarConfiguration : 1
296 ResolutionUnit : 2
297 PageNumber : 0 1
320 ColorMap : (Binary data 24 bytes, use -b option to extract)
---- Composite ----
- ImageSize : 256 256
- Megapixels : 0.065536

Related

Why does escpos hang when trying to connect to printer?

I have a SureMark 4610 printer that I'm trying to connect to, but for some reason it hangs when I connect. I've installed the necessary drivers for the printer. What could be the problem?
I'm using Windows 10 if that is of any use to you all.
Here's the printer device info
DEVICE ID 04b3:4535 on Bus 000 Address 019 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x0 Specified at interface
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x04b3
idProduct : 0x4535
bcdDevice : 0x216 Device 2.16
iManufacturer : 0x1 (c) Copyright IBM Corp. 2000
iProduct : 0x5 Printer Interface (Usage = 3500h, Usage Page = FF45h)
iSerialNumber : 0x3 060725163603C100193
bNumConfigurations : 0x1
CONFIGURATION 1: 0 mA ====================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x22 (34 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0x40 Self Powered
bMaxPower : 0x0 (0 mA)
INTERFACE 1: Human Interface Device ====================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x1
bAlternateSetting : 0x0
bNumEndpoints : 0x1
bInterfaceClass : 0x3 Human Interface Device
bInterfaceSubClass : 0x0
bInterfaceProtocol : 0x0
iInterface : 0x5 Printer Interface (Usage = 3500h, Usage Page = FF45h)
ENDPOINT 0x82: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x82 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x10 (16 bytes)
bInterval : 0x4
Here is the simple code
from escpos import printer
p = printer.Usb(idVendor=0x04B3, idProduct=0x4535, timeout=10, in_ep=0x82)
print(p.idProduct)
I cannot get to the print because it just hangs indefinitely at line 2.

ffmpeg conversion: Twitter rejects video with "Not valid video"

I have an app that uploads via twitter api chunked upload, and it finally works with photos.
However, I am trying to get it to work with videos.
Uploading didnt work out-of-the-box even though the video format is mp4. The twitter guidelines for uploads are these
With that in mind, I have this ffmpeg command so far:
ffmpeg -i in.mp4 -vf \"scale=1280:720\" -b:v 5000K -minrate 5000K -maxrate 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -profile:v high -pix_fmt yuv420p -strict -2 -ac 2 -acodec aac out.mp4
I still get "Not valid video", and I don't know why.
Here's my mediainfo output for out.mp4:
General
Count : 328
Count of stream of this kind : 1
Kind of stream : General
Kind of stream : General
Stream identifier : 0
Count of video streams : 1
Count of audio streams : 1
Video_Format_List : AVC
Video_Format_WithHint_List : AVC
Codecs Video : AVC
Video_Language_List : English
Audio_Format_List : AAC
Audio_Format_WithHint_List : AAC
Audio codecs : AAC LC
Audio_Language_List : English
Complete name : video-5e4405cd4348a5e4405cd434d2.mp4
File name : video-5e4405cd4348a5e4405cd434d2
File extension : mp4
Format : MPEG-4
Format : MPEG-4
Format/Extensions usually used : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
Commercial name : MPEG-4
Format profile : Base Media
Internet media type : video/mp4
Codec ID : isom
Codec ID : isom (isom/iso2/avc1/mp41)
Codec ID/Url : http://www.apple.com/quicktime/download/standalone.html
CodecID_Compatible : isom/iso2/avc1/mp41
Codec : MPEG-4
Codec : MPEG-4
Codec/Extensions usually used : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
File size : 52664272
File size : 50.2 MiB
File size : 50 MiB
File size : 50 MiB
File size : 50.2 MiB
File size : 50.22 MiB
Duration : 79744
Duration : 1 min 19 s
Duration : 1 min 19 s 744 ms
Duration : 1 min 19 s
Duration : 00:01:19.744
Duration : 00:01:19:20
Duration : 00:01:19.744 (00:01:19:20)
Overall bit rate : 5283334
Overall bit rate : 5 283 kb/s
Frame rate : 30.000
Frame rate : 30.000 FPS
Frame count : 2390
Stream size : 88780
Stream size : 86.7 KiB (0%)
Stream size : 87 KiB
Stream size : 87 KiB
Stream size : 86.7 KiB
Stream size : 86.70 KiB
Stream size : 86.7 KiB (0%)
Proportion of this stream : 0.00169
HeaderSize : 40
DataSize : 52575500
FooterSize : 88732
IsStreamable : No
File last modification date : UTC 2020-02-12 14:05:37
File last modification date (local) : 2020-02-12 15:05:37
Writing application : Lavf57.83.100
Writing application : Lavf57.83.100
Video
Count : 342
Count of stream of this kind : 1
Kind of stream : Video
Kind of stream : Video
Stream identifier : 0
StreamOrder : 0
ID : 1
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format/Url : http://developers.videolan.org/x264.html
Commercial name : AVC
Format profile : High#L3.1
Format settings : CABAC / 4 Ref Frames
Format settings, CABAC : Yes
Format settings, CABAC : Yes
Format settings, ReFrames : 4
Format settings, ReFrames : 4 frames
Internet media type : video/H264
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Codec : AVC
Codec : AVC
Codec/Family : AVC
Codec/Info : Advanced Video Codec
Codec/Url : http://developers.videolan.org/x264.html
Codec/CC : avc1
Codec profile : High#L3.1
Codec settings : CABAC / 4 Ref Frames
Codec settings, CABAC : Yes
Codec_Settings_RefFrames : 4
Duration : 79667
Duration : 1 min 19 s
Duration : 1 min 19 s 667 ms
Duration : 1 min 19 s
Duration : 00:01:19.667
Duration : 00:01:19:20
Duration : 00:01:19.667 (00:01:19:20)
Bit rate : 5000000
Bit rate : 5 000 kb/s
Width : 1280
Width : 1 280 pixels
Height : 720
Height : 720 pixels
Sampled_Width : 1280
Sampled_Height : 720
Pixel aspect ratio : 1.000
Display aspect ratio : 1.778
Display aspect ratio : 16:9
Rotation : 0.000
Frame rate mode : CFR
Frame rate mode : Constant
FrameRate_Mode_Original : VFR
Frame rate : 30.000
Frame rate : 30.000 FPS
Frame count : 2390
Resolution : 8
Resolution : 8 bits
Colorimetry : 4:2:0
Color space : YUV
Chroma subsampling : 4:2:0
Chroma subsampling : 4:2:0
Bit depth : 8
Bit depth : 8 bits
Scan type : Progressive
Scan type : Progressive
Interlacement : PPF
Interlacement : Progressive
Bits/(Pixel*Frame) : 0.181
Stream size : 51297022
Stream size : 48.9 MiB (97%)
Stream size : 49 MiB
Stream size : 49 MiB
Stream size : 48.9 MiB
Stream size : 48.92 MiB
Stream size : 48.9 MiB (97%)
Proportion of this stream : 0.97404
Writing library : x264 - core 152 r2854 e9a5903
Writing library : x264 core 152 r2854 e9a5903
Encoded_Library_Name : x264
Encoded_Library_Version : core 152 r2854 e9a5903
Encoding settings : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=abr / mbtree=1 / bitrate=5000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Language : en
Language : English
Language : English
Language : en
Language : eng
Language : en
Audio
Count : 275
Count of stream of this kind : 1
Kind of stream : Audio
Kind of stream : Audio
Stream identifier : 0
StreamOrder : 1
ID : 2
ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Commercial name : AAC
Format profile : LC
Format settings, SBR : No (Explicit)
Format settings, SBR : No (Explicit)
Codec ID : mp4a-40-2
Codec : AAC LC
Codec : AAC LC
Codec/Family : AAC
Codec/CC : 40
Duration : 79744
Duration : 1 min 19 s
Duration : 1 min 19 s 744 ms
Duration : 1 min 19 s
Duration : 00:01:19.744
Duration : 00:01:19:25
Duration : 00:01:19.744 (00:01:19:25)
Bit rate mode : CBR
Bit rate mode : Constant
Bit rate : 128257
Bit rate : 128 kb/s
Channel(s) : 2
Channel(s) : 2 channels
Channel positions : Front: L R
Channel positions : 2/0/0
ChannelLayout : L R
Samples per frame : 1024
Sampling rate : 48000
Sampling rate : 48.0 kHz
Samples count : 3827712
Frame rate : 46.875
Frame rate : 46.875 FPS (1024 SPF)
Frame count : 3738
Compression mode : Lossy
Compression mode : Lossy
Stream size : 1278470
Stream size : 1.22 MiB (2%)
Stream size : 1 MiB
Stream size : 1.2 MiB
Stream size : 1.22 MiB
Stream size : 1.219 MiB
Stream size : 1.22 MiB (2%)
Proportion of this stream : 0.02428
Language : en
Language : English
Language : English
Language : en
Language : eng
Language : en
Default : Yes
Default : Yes
Alternate group : 1
Alternate group : 1
Edit: Guess Ill add my PHP code too (This is chopped in a sensible manner because the actual file is very large):
// Set up Codebird
\Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret); // static, see README
$cb = \Codebird\Codebird::getInstance();
$cb->setToken($token, $token_secret);
$cb->setTimeout(60 * 1000); // 60 second request timeout
$video = new Video($path);
// Convert to the parameter required by twitter.
$converted = $video->convert();
$path = realpath('videos/' . $converted);
$file = fopen($path, 'rb');
$size = fstat($file)['size'];
$mime_type = mime_content_type($path);
$media = $cb->media_upload([
'command' => 'INIT',
'media_type' => $mime_type,
'media_category' => 'tweet_video',
'total_bytes' => $size,
]);
$success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx
if (!$success) {
throw new TwitterException("Failed to INIT upload for $path...");
}
// APPEND chunks to upload.
$mediaId = $media->media_id_string;
$segmentId = 0;
while (!feof($file)) {
echo "chunk #$segmentId....";
$chunk = fread($file, 512 * 1024); // caps out at 512 MB
echo "chunk size: ". strlen($chunk);
$media = $cb->media_upload([
'command' => 'APPEND',
'media_id' => $mediaId,
'segment_index' => $segmentId,
'media' => $chunk,
]);
$success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx
if (!$success) {
throw new TwitterException("Failed to APPEND to upload for $path, chunk $segmentId...");
}
$segmentId++;
}
// Close file and FINALIZE upload.
fclose($file);
echo "FINALIZING id $mediaId...";
$media = $cb->media_upload([
'command' => 'FINALIZE',
'media_id' => $mediaId,
]);
$success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx
if (!$success) {
var_dump($media);
throw new TwitterException("Failed to FINALIZE upload for $path...");
}
return $mediaId;
Video is my ffmpeg class, which I'll paste below, and $path is a URL leading to a perfectly valid mp4 video.
Video.php:
<?php
class VideoConversionException extends \Exception {}
class Video {
public $name;
public $converted;
public function __construct($name) {
self::clear();
$this->name = $name;
}
public function convert() {
$tmpVideo = 'video-' . uniqid() . uniqid() . '.mp4';
$videoPath = 'videos/' . $tmpVideo;
//$ffmpeg = "ffmpeg -i https://tvcanarias.acfipress.com/BC_190907_gc_teror.mp4 -vf "scale=1280:720" -b:v 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -acodec aac output_video.mp4";
$ffmpeg = "ffmpeg -i {$this->name} -vf \"scale=1280:720\" -b:v 5000K -minrate 5000K -maxrate 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -profile:v high -pix_fmt yuv420p -strict -2 -ac 2 -acodec aac $videoPath";
//$ffmpeg = "ffmpeg -i {$this->name} -pix_fmt yuv420p -vcodec libx264 -vf scale=640:-1 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100 -ac 2 -strict experimental -r 30 $videoPath";
$output = [];
exec($ffmpeg, $output, $status);
if ($status != 0) {
//die("Couldnt run ffmpeg. (Error code: #$status)");
throw new VideoConversionException("Couldn't run ffmpeg. (Error code: #$status)");
}
$this->converted = $tmpVideo;
return $tmpVideo;
}
public function shredConverted() {
// delete video.
#unlink("videos/{$this->$converted}");
}
public static function clear() {
// We can't really shred videos right away as they might be still uploading.
// Therefore, every time this library is used, we will just delete videos older
// than, say, an hour.
$files = scandir('videos');
$curtime = time();
foreach ($files as $file) {
if ($file == '.gitignore' || $file == '.' || $file == '..') {
continue;
}
$mtime = filemtime("videos/$file");
$diff = $curtime - $mtime;
$overAnHour = $diff > (60 * 60);
if ($overAnHour) {
#unlink("videos/$file");
}
}
}
}

esp8266 RTOS blink example doesn't work

I have a problem with the RTOS firmware on the esp8266 (I have a esp12e), after flashing the firmware, reading from uart, it keeps stuck with those lines:
ets Jan 8 2013,rst cause:2, boot mode:(3,0)
load 0x40100000, len 31584, room 16
tail 0
chksum 0x24
load 0x3ffe8000, len 944, room 8
tail 8
chksum 0x9e
load 0x3ffe83b0, len 1080, room 0
tail 8
chksum 0x60
csum 0x60
Now I will explain my HW setup:
GPIO15 -> Gnd
EN -> Vcc
GPIO0 -> Gnd (when flashing)
GPIO0 -> Vcc (normal mode)
For the toolchain I've followed this tutorial and it works well:
http://microcontrollerkits.blogspot.it/2015/12/esp8266-eclipse-development.html
Then I started doing my RTOS blink example, I post my user_main.c code here:
#include "esp_common.h"
#include "gpio.h"
void task2(void *pvParameters)
{
printf("Hello, welcome to client!\r\n");
while(1)
{
// Delay and turn on
vTaskDelay (300/portTICK_RATE_MS);
GPIO_OUTPUT_SET (5, 1);
// Delay and LED off
vTaskDelay (300/portTICK_RATE_MS);
GPIO_OUTPUT_SET (5, 0);
}
}
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don't know which sector is free in user's application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 user_rf_cal_sector_set(void)
{
flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void user_init(void)
{
uart_init_new();
printf("SDK version:%s\n", system_get_sdk_version());
// Config pin as GPIO5
PIN_FUNC_SELECT (PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
xTaskCreate(task2, "tsk2", 256, NULL, 2, NULL);
}
I also post the flash command, the first executed one time, the second every time I modify the code:
c:/Espressif/utils/ESP8266/esptool.exe -p COM3 write_flash -ff 40m -fm qio -fs 32m 0x3FC000 c:/Espressif/ESP8266_RTOS_SDK/bin/esp_init_data_default.bin 0x3FE000 c:/Espressif/ESP8266_RTOS_SDK/bin/blank.bin 0x7E000 c:/Espressif/ESP8266_RTOS_SDK/bin/blank.bin
c:/Espressif/utils/ESP8266/esptool.exe -p COM3 -b 256000 write_flash -ff 40m -fm qio -fs 32m 0x00000 firmware/eagle.flash.bin 0x40000 firmware/eagle.irom0text.bin
There is something wrong? I really don't understand why it doesn't work.
When I try the NON-OS example they works very well.
I had the same problem as you. This issue is caused by the incorrect address of the eagle.irom0text.bin .
So I changed the address of the eagle.irom0text.bin from 0x40000 (0x10000) to 0x20000 and it worked well for me.
[RTOS SDK version: 1.4.2(f57d61a)]
The correct flash codes in the common_rtos.mk (ESP-12E)
for flashinit
flashinit:
$(vecho) "Flash init data default and blank data."
$(ESPTOOL) -p $(ESPPORT) write_flash $(flashimageoptions) 0x3fc000 $(SDK_BASE)/bin/esp_init_data_default.bin
$(ESPTOOL) -p $(ESPPORT) write_flash $(flashimageoptions) 0x3fe000 $(SDK_BASE)/bin/blank.bin
for flash:
flash: all
#ifeq ($(app), 0)
$(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) 0x00000 $(FW_BASE)/eagle.flash.bin 0x20000 $(FW_BASE)/eagle.irom0text.bin
else
ifeq ($(boot), none)
$(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) 0x00000 $(FW_BASE)/eagle.flash.bin 0x20000 $(FW_BASE)/eagle.irom0text.bin
else
$(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) $(addr) $(FW_BASE)/upgrade/$(BIN_NAME).bin
endif
endif

Getting the stack pointer content using Intel's PinTool

Using objdump utility, we are able to retrieved the relative address of a variable for example consider a simple C program:
Source-code:
#include<stdio.h>
void do_stuff(int my_arg){
int my_local=my_arg+2;
int i;
for(i=0;i<my_local;i++)
printf("i=%d\n",i);
}
int main(){
do_stuff(2);
return 0;
}
Compile with gcc :
$ gcc -g example.c -o example
Run objdump utility with dwarf flag for ELF information.
$objdump --dwarf=info example
Output:
Contents of the .debug_info section:
Compilation Unit # offset 0x0:
Length: 0xd3 (32-bit)
Version: 4
Abbrev Offset: 0x0
Pointer Size: 8
<0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
<c> DW_AT_producer : (indirect string, offset: 0x0): GNU C 4.8.4 -mtune=generic -march=x86-64 -g -fstack-protector
<10> DW_AT_language : 1 (ANSI C)
<11> DW_AT_name : (indirect string, offset: 0xcd): sample1.c
<15> DW_AT_comp_dir : (indirect string, offset: 0x7e): /home/bernard/PhD/TEJAS/tejas_installation_kit/Tejas-Simulator/Tejas/benchmark
<19> DW_AT_low_pc : 0x40052d
<21> DW_AT_high_pc : 0x54
<29> DW_AT_stmt_list : 0x0
<1><2d>: Abbrev Number: 2 (DW_TAG_base_type)
<2e> DW_AT_byte_size : 8
<2f> DW_AT_encoding : 7 (unsigned)
<30> DW_AT_name : (indirect string, offset: 0x47): long unsigned int
<1><34>: Abbrev Number: 2 (DW_TAG_base_type)
<35> DW_AT_byte_size : 1
<36> DW_AT_encoding : 8 (unsigned char)
<37> DW_AT_name : (indirect string, offset: 0x62): unsigned char
<1><3b>: Abbrev Number: 2 (DW_TAG_base_type)
<3c> DW_AT_byte_size : 2
<3d> DW_AT_encoding : 7 (unsigned)
<3e> DW_AT_name : (indirect string, offset: 0xde): short unsigned int
<1><42>: Abbrev Number: 2 (DW_TAG_base_type)
<43> DW_AT_byte_size : 4
<44> DW_AT_encoding : 7 (unsigned)
<45> DW_AT_name : (indirect string, offset: 0x4c): unsigned int
<1><49>: Abbrev Number: 2 (DW_TAG_base_type)
<4a> DW_AT_byte_size : 1
<4b> DW_AT_encoding : 6 (signed char)
<4c> DW_AT_name : (indirect string, offset: 0x64): signed char
<1><50>: Abbrev Number: 2 (DW_TAG_base_type)
<51> DW_AT_byte_size : 2
<52> DW_AT_encoding : 5 (signed)
<53> DW_AT_name : (indirect string, offset: 0xf1): short int
<1><57>: Abbrev Number: 3 (DW_TAG_base_type)
<58> DW_AT_byte_size : 4
<59> DW_AT_encoding : 5 (signed)
<5a> DW_AT_name : int
<1><5e>: Abbrev Number: 2 (DW_TAG_base_type)
<5f> DW_AT_byte_size : 8
<60> DW_AT_encoding : 5 (signed)
<61> DW_AT_name : (indirect string, offset: 0x75): long int
<1><65>: Abbrev Number: 2 (DW_TAG_base_type)
<66> DW_AT_byte_size : 8
<67> DW_AT_encoding : 7 (unsigned)
<68> DW_AT_name : (indirect string, offset: 0xfb): sizetype
<1><6c>: Abbrev Number: 2 (DW_TAG_base_type)
<6d> DW_AT_byte_size : 1
<6e> DW_AT_encoding : 6 (signed char)
<6f> DW_AT_name : (indirect string, offset: 0x6b): char
<1><73>: Abbrev Number: 4 (DW_TAG_subprogram)
<74> DW_AT_external : 1
<74> DW_AT_name : (indirect string, offset: 0x59): do_stuff
<78> DW_AT_decl_file : 1
<79> DW_AT_decl_line : 2
<7a> DW_AT_prototyped : 1
<7a> DW_AT_low_pc : 0x40052d
<82> DW_AT_high_pc : 0x3f
<8a> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)
<8c> DW_AT_GNU_all_tail_call_sites: 1
<8c> DW_AT_sibling : <0xb9>
<2><90>: Abbrev Number: 5 (DW_TAG_formal_parameter)
<91> DW_AT_name : (indirect string, offset: 0xd7): my_arg
<95> DW_AT_decl_file : 1
<96> DW_AT_decl_line : 2
<97> DW_AT_type : <0x57>
<9b> DW_AT_location : 2 byte block: 91 5c (DW_OP_fbreg: -36)
<2><9e>: Abbrev Number: 6 (DW_TAG_variable)
<9f> DW_AT_name : (indirect string, offset: 0x3e): my_local
<a3> DW_AT_decl_file : 1
<a4> DW_AT_decl_line : 3
<a5> DW_AT_type : <0x57>
<a9> DW_AT_location : 2 byte block: 91 6c (DW_OP_fbreg: -20)
<2><ac>: Abbrev Number: 7 (DW_TAG_variable)
<ad> DW_AT_name : i
<af> DW_AT_decl_file : 1
<b0> DW_AT_decl_line : 4
<b1> DW_AT_type : <0x57>
<b5> DW_AT_location : 2 byte block: 91 68 (DW_OP_fbreg: -24)
<2><b8>: Abbrev Number: 0
<1><b9>: Abbrev Number: 8 (DW_TAG_subprogram)
<ba> DW_AT_external : 1
<ba> DW_AT_name : (indirect string, offset: 0x70): main
<be> DW_AT_decl_file : 1
<bf> DW_AT_decl_line : 9
<c0> DW_AT_type : <0x57>
<c4> DW_AT_low_pc : 0x40056c
<cc> DW_AT_high_pc : 0x15
<d4> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)
<d6> DW_AT_GNU_all_tail_call_sites: 1
<1><d6>: Abbrev Number: 0
I want to do as follows:-
I want to retrieved the address of a variable , say my_local. First I will look into DW_TAG_Variable, then DW_AT_name, DW_AT_location which was given by base register at function -20 from the top.
Question:
How to know the content of base register at run time.
Can we implement it using PinTool.
Question in broader sense:
I want variable my_local map to which address in memory given information from dwarf and Pintool.
Thanks in Advance.
You should be able to use Pin's SafeCopy function to access app memory and copy it into pintool memory.
But as noted in the comments, it's not always possible to know where variable values are stored reliably so keep this in mind.

Is extra space allocated for variant records?

I'm working with the variant record below. The variable instance is Kro_Combi. SizeOf(Kro_Combi) reports 7812 bytes. SizeOf(Kro_Combi.data) reports 7810 bytes.
The sum of the SizeOf of all the other data structures composing the "non-directmode" case of the variant record also adds to 7810 bytes.
Why is there a two byte difference? I would like to have the two variant exactly overlay each other.
TKro_Combi = record
case directmode:boolean of
true : (
data : array[0..7809] of byte
);
false : (
Combi_Name : array[0..23] of char; //24
Gap1 : array[0..63] of byte; // 24-87 (64)
Ins_Effect_Group : array[1..12] of TIns_Effect_Params; //74 each, (Ins_Effect_Data=9 bytes) 74*12 = 888
Mast_Effect_Params : array[0..229] of byte; // 976-1205 : 230 bytes
Vect_Aud__Drum_Params : array[0..97] of byte; //1206-1303 : 98 bytes
Karma_Common : array[0..509] of byte; //1304-1813 : 510 bytes
Karma_Module : array[0..3] of TKarma_Module; //1814-2557 : 744 bytes each Total span 1814 - 4789 = 2976 bytes total
Common_Params : array[0..11] of byte; //4790-4801 = 12 bytes
Timbre_Group : array[1..16] of TTimbre_Params; ) // 4802 -4989 = 188 bytes each, 16 Timbres, 4802-7809 = 3008 bytes total for all
end;
First of all, there needs to be space for the directmode field. If you really want the record to have size 7810 bytes then you should remove that field. The other byte will be due to internal alignment and padding of the false part of the variant record. I can't yet quite work out where it comes from. No matter, you simply want to use a packed record to avoid any padding bytes.
TKro_Combi = packed record
case boolean of
true : (
data : array[0..7809] of byte
);
false : (
Combi_Name : array[0..23] of char; //24
Gap1 : array[0..63] of byte; // 24-87 (64)
Ins_Effect_Group : array[1..12] of TIns_Effect_Params; //74 each, (Ins_Effect_Data=9 bytes) 74*12 = 888
Mast_Effect_Params : array[0..229] of byte; // 976-1205 : 230 bytes
Vect_Aud__Drum_Params : array[0..97] of byte; //1206-1303 : 98 bytes
Karma_Common : array[0..509] of byte; //1304-1813 : 510 bytes
Karma_Module : array[0..3] of TKarma_Module; //1814-2557 : 744 bytes each Total span 1814 - 4789 = 2976 bytes total
Common_Params : array[0..11] of byte; //4790-4801 = 12 bytes
Timbre_Group : array[1..16] of TTimbre_Params; ) // 4802 -4989 = 188 bytes each, 16 Timbres, 4802-7809 = 3008 bytes total for all
end;

Resources